System.DateTime doesn't convert to SqlDbType.DateTime

N

Narshe

If I use this simple code

SqlParameter param = new SqlParameter( "@param", SqlDbType.DateTime );
param.Value = System.DateTime.Now;

param.Value is "11/2/2005" rather than "11/2/2005 10:42:15 AM"

How do I get it so the sql value is the same as the C# value, and not
truncated?
 
N

Nicholas Paldino [.NET/C# MVP]

Narshe,

Are you basing this on what you see in the debugger for Value? Have you
looked at the object itself and found that the time values are actually
zero? When you persist it ack to the database, does it not include the
time?

If this is the case, then have you tried using the constructor to
SqlDateTime which takes a DateTime parameter, and setting it like that?

Hope this helps.
 
N

Narshe

I have not tried that constructor yet. I noticed that doing a
ToString() on the value gives the correct value, but the short value is
persisited across to the db also.

I'll give that other constructor a try also.
 

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