Data Type Mismatch in Criteria Expression | Access, OleDb, C #

I am reading / updating data from MS Access using C #. My code is:

public static void UpdateLastLogin(int userid, DateTime logintime) ///logintime = DateTime.Now
{
    string sql = @"UPDATE [Customers] SET [LastLogin]=?";
    OleDbParameter[] prms = new OleDbParameter[] { 
     new OleDbParameter("@LastLogin",logintime)
    };
    using (DAL dal = new DAL())
    {
        dal.UpdateRow(sql, false, prms);
    }
}

When it comes to dates, I have problems. This causes a โ€œdata type mismatch in the criteria expressionโ€. mistake. (I removed the WHERE clause for simplicity) Can I attach [LastLogin] =? single quotation mark, # characters .. doesn't help. Any guidance on how to handle DateTime objects with Access and OleDb would be greatly appreciated.

Thanks in advance.

+5
source share
6 answers

Known issue with OleDb and dates. Try to do something like:

OleDbParameter p = parameter as OleDbParameter;
if (null == p)
  parameter.DbType = DbType.DateTime;
else
  p.OleDbType = OleDbType.Date;

:

value.ToString("yyyy-MM-dd hh:mm:ss")
+4

,

OleDbCommand cmd = new OleDbCommand(qry, cnn);
cmd.Parameters.Add("datenow", OleDbType.Date);
cmd.Parameters["datenow"].Value = DateTime.Now;
+1

-, SQL:

"UPDATE Customers SET LastLogin=@LastLogin"

-, , , , ?? LastLogin logintime.

0

,

DateTime.Now.ToShortDateString() + ' ' + DateTime.Now.ToShortTimeString()

String (, , # )

0

"UPDATE Customers SET LastLogin='@LastLogin'"

@LastLogin

logintime.ToString("yyyy-MM-dd hh:mm:ss")

?

"UPDATE Customers SET LastLogin='" + logintime.ToString("yyyy-MM-dd hh:mm:ss") + "'"
0

"DBTYPE" , , 2. ...

prms[0].DbType = DbType.DateTime;

OleDbParameter() 7 , , , 1 .

0

All Articles