Question about DateTime

R

Roy

I have a DataRow dr retrieved from a database. One of the column is datetime
type. How do I convert it to DateTime structure in .NET?
DateTime dt = DateTime.Parse(dr[datetimecolumn].ToString()) will drop the
millisecond part. I want to keep the millisecond part.
 
M

Marc Gravell

You can probably just cast; try looking at
dr[datetimecolumn].GetType() to see what it actually is...

I suspect this will work:

DateTime dt = (DateTime)dr[datetimecolumn];

Marc
 
H

Hans Kesting

Roy wrote on 28-7-2008 :
I have a DataRow dr retrieved from a database. One of the column is datetime
type. How do I convert it to DateTime structure in .NET?
DateTime dt = DateTime.Parse(dr[datetimecolumn].ToString()) will drop the
millisecond part. I want to keep the millisecond part.

Try casting it directly to DateTime:
DateTime dt = (DateTime)dr[datetimecolumn];

but watch out for DbNull values, as that will cause a crash (you can't
cast a DbNull to DateTime).

Hans Kesting
 
M

Marc Gravell

What if I want to dynamically construct a TSQL and use it in the WHERE clause?
"WHERE datetimecolumn = " + dt.ToString() will not include the millisecond
either.
Thanks.

A good rule of thumb: never concatenate user input; use parameters
instead. This saves you from internationalisation (i18n) issues (does
130,000 mean "130 thousand", or "130 point zero"?), and from injection
attacks (http://xkcd.com/327/).

So your TSQL would be "WHERE datetimecolumn = @somearg", and you'd add
a parameter called "somearg" to the command's parameters collection.

Marc
 

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