Passing datetime as parameter throws Exception

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?
 
B

Brane Brodnik

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
 
N

Neil

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.
 
N

Neil

Sorry, I meant

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top