I'm trying to insert a System.DateTime into an Access database using a parameterized OleDbCommand in C#. However, it throws a Data type mismatch in criteria expression
exception.
Here is my code:
string statement = "INSERT INTO Log (SCTID, LogDateTime, Type, Message, Visible)" +
"VALUES (?, ?, ?, ?, ?);";
OleDbCommand insertCommand = new OleDbCommand(statement, connection);
// Add parameters to the command
insertCommand.Parameters.AddWithValue("@SCTID", OleDbType.Integer).Value = SCTID;
insertCommand.Parameters.AddWithValue("@LogDateTime", OleDbType.DBTime).Value = dateTime;
insertCommand.Parameters.AddWithValue("@Type", OleDbType.Integer).Value = (int)logType;
insertCommand.Parameters.AddWithValue("@Message", OleDbType.BSTR).Value = message;
insertCommand.Parameters.AddWithValue("@Visible", OleDbType.Boolean).Value = visible;
insertCommand.ExecuteNonQuery();
When I comment out the LogDateTime
line, the rest of it works. My problem is that no matter what I use for the DateTime type, it doesn't work. I've tried:
OleDbType.Date, OleDbType.DBDate, OleDBType.DBTimeStamp, DbType.Date, DbType.DateTime, DbType.DateTime2
I've also tried:
insertCommand.Parameters.AddWithValue("@LogDateTime", dateTime);
It doesn't work either. Nothing I've read through Google or SO works. Also, note that I do need both date and time, not just a date alone.