简体   繁体   中英

“Data type mismatch in criteria expression” error in Access SQL query from C#

I am getting below Error, When I put Multiple Conditions with WHERE Clause. Error:- Data type mismatch in criteria expression.

My Query:-

this.query = "UPDATE [Attendance] SET [TimeOut]='" + DateTime.Now.ToShortTimeString() + "' WHERE [Emp_Id]='" + txtEmpId.Text + "'and[Date]='" + this.Date + "'";

Access SQL tends to be rather flexible when accepting Date/Time values as strings. However, since you really should be using a parameterized query anyway because

  • they're safer (by avoiding SQL Injection issues),
  • you don't have to mess with delimiters for date and text values,
  • you don't have to worry about escaping quotes within text values, and
  • they handle dates properly so your code doesn't mangle dates on machines set to dd-mm-yyyy format,

consider using the following approach

this.query = "UPDATE [Attendance] SET [TimeOut]=? WHERE [Emp_Id]=? AND [Date]=?";
cmd.CommandText = this.query;
cmd.Parameters.AddWithValue("?", DateTime.Now.ToString("H:mm:ss"));
cmd.Parameters.AddWithValue("?", txtEmpId.Text);
cmd.Parameters.AddWithValue("?", this.Date);
cmd.ExecuteNonQuery();

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