Passing datetime as parameter throws Exception

  • Thread starter Thread starter Brane Brodnik
  • Start date Start date
B

Brane Brodnik

I have data field MyTime of type Date/Time in MS Access table Test. I want
to add record into this table with value passed as parameter.

I do the following in C#:

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = MyConnection;
cmd.CommandText = "INSERT INTO Test (MyTime) VALUES(?);";
cmd.Parameters.Add("MyTime", DateTime.Now);
cmd.ExecuteNonQuery();

Last statement throws an exception with message
"Data type mismatch in criteria expression."

If I pass only date part to parameter, for example:
cmd.Parameters.Add("MyTime", DateTime.Today);
it works fine.

How can I create and use parameter that wold pass both date and time parts
to date/time field?
 
Sorry, read
cmd.Parameters.Add("MyTime", DateTime.Now);
as
cmd.Parameters.Add(new OleDbParameter("MyTime", DateTime.Now));
and
cmd.Parameters.Add("MyTime", DateTime.Today);
as
cmd.Parameters.Add(new OleDbParameter("MyTime", DateTime.Today));

Brane Brodnik
 
Don't you mean

OleDbParameter prm = new OleDbParameter("MyTime", OleDbType.DateTime);
cmd.Parameters.Add(prm);
prm.Value = DateTime.Now;
etc.

If not, then try it.
 
Sorry, I meant

OleDbParameter prm = new OleDbParameter("MyTime", OleDbType.Date);
cmd.Parameters.Add(prm);
prm.Value = DateTime.Now;
 
Back
Top