简体   繁体   中英

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

What is the correct syntax of date between 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 . Use parameterized queries to let ADO.Net handle this for you. As a bonus, this will protect you against SQL Injection attacks and also generally perform better.

Additionally, it does not make sense at all to use LIKE with BETWEEN . For that matter, BETWEEN itself is often poor practice for date comparisons, because it is inclusive on both ends of the range. 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. 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? 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#. 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.

And finally use this in the datetimes to avoid formatting issues.

dateTimePicker1.Value.ToString(CultureInfo.InvariantCulture)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM