Inserting a date/time value in Access using an OleDbParameter
Asked Answered
C

4

6

I'm trying to do an insert in oledb(ms access database) the field called objectdate is date/time

the code i use to add the parameter is this, but i'm getting error.

  OleDbParameter objectdate = new OleDbParameter("@objectdate", OleDbType.DBDate);
  objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate);

the error:

Data type mismatch in criteria expression.

Capercaillie answered 23/9, 2011 at 0:8 Comment(2)
Please don't put " c#" at the end of your title. On Stack Overflow, we use tags for that.Streamlet
I just copied and pasted those exact statements into C# (VS 2010) and they worked fine for me. Voting to close as "off-topic (...problem that can no longer be reproduced...)".Crinkly
P
10

OleDB doesn't like milliseconds in the datetime parameters. If you remove the milliseconds it will go ok. See also: How to truncate milliseconds off of a .NET DateTime.

Pozsony answered 15/5, 2012 at 11:53 Comment(1)
M
0

You could use.

   OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime);
   objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate);

or use the Ole Automation version of the date.

OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime);
       objectdate.Value = DateTime.Now.ToOADate(); cmd.Parameters.Add(objectdate);

Or you could enter the datetime as a literal since the Datetime.ToString() removes the milliseconds that access can't work with.

cmd.Parameters.AddWithValue("@objectdate", DateTime.Now.ToString());

this should work.

Mccrae answered 23/9, 2011 at 0:20 Comment(9)
doesn't work, it say Failed to convert parameter value from a DbType to a DateTime.Capercaillie
@Capercaillie .. could you post the code you used? just in the comments here.Mccrae
@Capercaillie i wanted you to replace OleDbType.DBDate with DbType.DateTimeMccrae
the code and the error I'm getting: DateTime dateobjectdate = DateTime.Now; OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime); objectdate.Value = dateobjectdate; cmd.Parameters.Add(objectdate); Data type mismatch in criteria expression.Capercaillie
Also i've tried the next lines with no luck, btw how can indent?? __________________________________________________________________ OleDbParameter objectdate = new OleDbParameter("@objectdate", OleDbType.Date); objectdate.Value = DateTime.Now; cmd.Parameters.Add(objectdate); ________________________________ Data type mismatch in criteria expression. OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime); objectdate.Value = DateTime.Now.ToString(); cmd.Parameters.Add(objectdate); Data type mismatch in criteria expression.Capercaillie
@Capercaillie this seems to be a known issue. could you try using DbType.Date ? just to see if it works.Mccrae
@Capercaillie i've added other samples that should work. please try them out.Mccrae
i tested the 3 method, and still i'm getting the: Data type mismatch in criteria expression. the access field is datetime, i don't know why it doesn't workCapercaillie
@Capercaillie why don't you just call the Date() function in your sql? access understands that.Mccrae
V
0

The sentence:

OleDbParameter objectdate = new OleDbParameter("@objectdate", DbType.DateTime);

is not acepted in visual basic 2008, I use like this:

ordeen.Parameters.Add(New OleDb.OleDbParameter("objectdate", DbType.DateTime))
ordeen.Parameters("objectdate").Value=object.text   'but its not run

the next sentence only functional in sqlserver:

cmd.Parameters.AddWithValue("@objectdate", DateTime.Now.ToString());

the problem in Access continued yet

Vhf answered 24/9, 2013 at 17:6 Comment(0)
S
0

When using OleDb in .netstandard2.0 you can add using the .AddWithValue with just a key value pair. The type is inferred from the value object:

cmd.Parameters.AddWithValue("@objectdate", DateTime.Now)

Do not convert to string because that would destroy the ability to infer type.

Skean answered 22/6, 2022 at 17:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.