简体   繁体   English

如何使用 LIKE % C# 获取开始日期和结束日期

[英]How can i get the Start Date and End Date with LIKE % C#

What is the correct syntax of date between like ?之间是什么日期的正确语法like This is my example code.这是我的示例代码。 I get an error on the query我在查询中遇到错误

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    MySqlDataAdapter sda = new MySqlDataAdapter(
        "SELECT `empID`, `Name`, `Date`, `empIn`, `empOut`, `workhours`, `workhoursTotal`, `workhoursLate`, `workhoursLateTotal`, `overtime`, `minuteOvertime`, `Reason`, `undertime`, `undertimeTotalMin`, `status`, `Payslip` FROM `attendance`"
        + " WHERE  Date BETWEEN LIKE '" 
        + dateTimePicker1.Value.ToString("yyyy-MM-dd") 
        + "%' AND Date LIKE'" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "%'", con);

    DataTable data = new DataTable();
    sda.Fill(data);
    dataGridView1.DataSource = data;
}

If you're converting DateTime values to strings for use in SQL, you're doing something VERY WRONG .如果您将 DateTime 值转换为字符串以在 SQL 中使用,那么您正在做一些非常错误的事情。 Use parameterized queries to let ADO.Net handle this for you.使用参数化查询让 ADO.Net 为您处理这个问题。 As a bonus, this will protect you against SQL Injection attacks and also generally perform better.作为奖励,这将保护您免受 SQL 注入攻击,并且通常性能更好。

Additionally, it does not make sense at all to use LIKE with BETWEEN .此外,将LIKEBETWEEN一起使用完全没有意义。 For that matter, BETWEEN itself is often poor practice for date comparisons, because it is inclusive on both ends of the range.就此而言, BETWEEN本身对于日期比较来说通常是不好的做法,因为它包含在范围的两端。 Better practice is to compare where the date is >= to the start of the range, and < (without the = ) the first moment of the next day at the end of the range.更好的做法是比较日期>=与范围开始的位置,以及< (没有= )范围结束时第二天的第一时刻。

It also looks like you're trying to re-use the same connection object throughout your application or form.看起来您还试图在整个应用程序或表单中重复使用相同的连接对象。 Don't do that.不要那样做。 ADO.Net has a featured called connection pooling that will handle this for you in a way that is more effective. ADO.Net 具有称为连接池的功能,它将以更有效的方式为您处理此问题。 You really do want to create a new connection object for most queries.您确实希望为大多数查询创建一个新的连接对象。

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    var result = new DataTable();
    string sql = @"
        SELECT `empID`, `Name`, `Date`, `empIn`, `empOut`, `workhours`, `workhoursTotal`, 
           `workhoursLate`, `workhoursLateTotal`, `overtime`, `minuteOvertime`, `Reason`, 
           `undertime`, `undertimeTotalMin`, `status`, `Payslip` 
        FROM `attendance`
        WHERE `Date` > @StartDate AND `Date` <= @EndDate";

    using (var con = new MySqlConnection("connection string here"))
    using (var cmd = new MySqlCommand(sql, con))
    using (var sda = new MySqlDataAdapter(cmd))
    {
        cmd.Parameters.Add("@StartDate", MySqlDbType.DateTime).Value =  dateTimePicker1.Value.Date;
        cmd.Parameters.Add("@StartDate", MySqlDbType.DateTime).Value =  dateTimePicker2.Value.Date.AddDays(1);
 
        sda.Fill(result);
    }
    dataGridView1.DataSource = result;
}

First than all, are you sure your dateTimePicker has information?首先,您确定您的 dateTimePicker 有信息吗? is not a good practice to send the value directly to the database.将值直接发送到数据库不是一个好习惯。 Second the code has many errors:其次,代码有很多错误:

MySqlDataAdapter sda = new MySqlDataAdapter("SELECTempID,Name,Date,empIn,empOut,workhours,workhoursTotal,workhoursLate,workhoursLateTotal,overtime,minuteOvertime,Reason,undertime,undertimeTotalMin,status,PayslipFROMattendance` WHERE Date BETWEEN LIKE '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "%' AND Date LIKE'" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "%'", con);

There is a missing space after the SELECT, before and after the FROM, in the last Like, please check you code in MySQL and then copy it in c#. SELECT 后面,FROM 之前和之后有一个空格,在最后一个 Like 中,请检查你在 MySQL 中的代码,然后将其复制到 c# 中。 Should look like this:应该是这样的:

    MySqlDataAdapter sda = new MySqlDataAdapter("SELECT empID, Name, Date, empIn, empOut, workhours, workhoursTotal, workhoursLate, workhoursLateTotal, overtime, minuteOvertime, Reason, undertime, undertimeTotalMin, status, Payslip 
FROM Mattendance WHERE Date BETWEEN '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' AND '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "' ", con);

Probably there are more syntax error but you could check them by yourselve.可能有更多的语法错误,但你可以自己检查它们。

Third make no sense to use "Like" with datetimes, so just use BETWEEN.第三个在日期时间中使用“Like”是没有意义的,所以只使用 BETWEEN。

And finally use this in the datetimes to avoid formatting issues.最后在日期时间使用它来避免格式​​问题。

dateTimePicker1.Value.ToString(CultureInfo.InvariantCulture)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中获取该年的周开始日期(星期一)和结束日期(星期日)列表 - How to get the list of week start date (Monday) and end date (Sunday) for the year in C# 如何从C#中的日期计算一天的开始和一天的结束 - How to calculate start of day and end of day from a date in c# 如何根据当前日期在 C# 中获取会计年度的开始和结束? - 使用 SSIS 脚本任务 - How to get the start and end of Fiscal Year in C# based on current date? - Using SSIS Script Task c# 根据日期列表查找开始日期和结束日期? - c# find start date and end date based on a list of dates? 如何在C#中从SQL正确获取日期? - How I can get date correctly from sql in c#? 如何在 c# 中获得今天的犹太日期? - How can I get today's Jewish date in c#? 如何在C#中获取驱动程序的日期和版本? - How can I get the date and version of drivers in C#? 如何在C#中以正确的时间检索当前周的开始日期和结束日期? - How to retrieve start date and end date with correct time for current week in C#? 如何通过使用星期数和年份来获取星期的开始和结束日期并将这些日期插入C#中的mysql表中 - How to get start and end date of week by using week number and year and insert those dates into mysql table in c# 如何从前端(Angular)向C#.net datetime(webAPI)发送日期? - How can I send a date from my front end (Angular) to C# .net datetime (webAPI)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM