Using "OleDbParameter" with a DateTime type

G

Guest

I'm performing an Update query using an OleDbCommand. The SQL in that command
looks like...

UPDATE FeedbackT SET DateTime=?, Comment=?, PressOrg=? WHERE Type = 'UpdateMe'

When I try to populate the DataTime parameter using

P = new OleDbParameter("@DateTime", (Object)DateTime.Now);

I get a runtime error. Any idea what I might be doing wrong?

Alex
 
C

Cor Ligthert [MVP]

Alex,

We have at the moment a minor problem on our website. (Will be done at
something as gmt 12:00 probably), with linked pages. Therefore the sample in
this text.

This sample is to show how to use SQL Server parameters

Be aware that this shows it very simple.
For a next selection the parameter collection should be used.

--------------------------------------------------------------------------------

Dim cmd As New SQLCommand("SELECT * FROM MyTable WHERE MyDate BETWEEN
@BeginDate And @EndDate", Conn)
cmd.Parameters.Add("@BeginDate", cdate(txtBeginDate.text))
cmd.Parameters.Add("@EndDate", cdate(txtEndDate.text))

I hope this helps,

Cor
 
G

Guest

Cor -

Thanks but I'm still having a problem. Maybe this is because you are using a
SQLCommand and I'm using an OleDbCommand? Not sure, but whenever I try to use
any parameters which are DateTimes, even when running your exact example, I
get an "OleDb Exception - Date Type Mismatch in criteria expression". Below
is ALL of the code (pretty simple). Any ideas what I'm doing wrong?

//SELECT IN DATE RANGE...
SQL = "Select * from FeedbackT where SubmitDate BETWEEN @StartDate
and @EndDate";
DBC = new OleDbCommand(SQL, m_Conn);

P = new OleDbParameter("@StartDate",
(Object)(Convert.ToDateTime("5/1/2006")));
DBC.Parameters.Add(P);

P = new OleDbParameter("@EndDate", (Object)DateTime.Now);
DBC.Parameters.Add(P);

DBC.ExecuteReader();
 
G

Guest

OKAY, I've learned more - Turns out that I can make an OleDbParameter work
with an Access database DateTime column if I set the Parameter object "P" as
follows...

P.DbType = DbType.Date;

But the problem with doing this is that then only the Date portion (but not
the Time portion) of the value gets written to the Access database column. If
I set it to

P.DbType = DbType.DateTime;

I get a runtime Data Type Mismatch. There MUST be some way to fix this,
right???

Alex
 

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