c# - Exception using DateTime in a parameterized OleDbCommand -


I am trying to insert a system dot time in an Access database using the parameterized OleDbCommand in C #. However, this data type mismatch in the criteria expression exception.

Here is my code:

  string description = "log" SCTID, log date time, type, message, visible) "+" value (?,? ,?,?,?); "; OleDbCommand insertCommand = New OleDbCommand (statement, connection); // Command insertCommand.Parameters.AddWithValue (" @ SCTID ", OleDbType.Integer) parameter. Value = SCTID; InsertCommand.Parameters.AddWithValue (" @loget time ", 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 use the logdetetime line I exclude, all else works. My problem is that any thing which I use it for the datetime type, it does not work I have tried:

OleDbType.Date, OleDbType.DBDate, OleDBType.DBTimeStamp, DbType.Date, DbType.DateTime, DbType DateTime2

I also tried:

  insertCommand.Parameters.AddWithValue ("@loget time", datetime);  

It does not work either I have not read anything through Google or SO functions. Besides, note that I need both date and time, only one date No.

  InsertCommand.Parameters.AddWithValue ("@ SCTID", OleDbType.Integer) .Value = SCTID; ...  

This is a very strange way of using the AddWithValue . It does not have the second parameter type - it is of the value you want, as per the given, you use the integral value of the calculation class member OleDbType.Integer , and then immediately the value By specifying the property, overwrite it. It should either:

  insertCommand.Parameters.AddWithValue ("@ SCTID", SCTID);  

or:

  insertCommand.Parameters.Add ("@ SCTID", OleDbType.Integer) .Value = SCTID;  

For the placeholders in the statement text, do you ? Why use , but then when using the names of the archive, do you use names?

In relation to the real problem - it seems that, and before you specify in your DateTime value, and using OleDbType.Date To reduce the milliseconds is to reduce.


Comments