[英]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
.此外,将LIKE
与BETWEEN
一起使用完全没有意义。 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.