471,330 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Question about DateTime

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.
Jul 28 '08 #1
4 1230
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
Jul 28 '08 #2
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
Jul 28 '08 #3
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.

"Hans Kesting" wrote:
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
Jul 28 '08 #4
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
Jul 28 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jan Danielsson | last post: by
reply views Thread by Ryan R. Tharp | last post: by
6 posts views Thread by Carl | last post: by
8 posts views Thread by Jimmie | last post: by
5 posts views Thread by Dinçer | last post: by
18 posts views Thread by Nick Z. | last post: by
3 posts views Thread by Ken H | last post: by
4 posts views Thread by Philippe Martin | last post: by
7 posts views Thread by JamesG | last post: by
3 posts views Thread by Microsoft | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.