469,645 Members | 1,971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Convert.ToDateTime

I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S
Nov 17 '05 #1
5 18310
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S

Nov 17 '05 #2
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...

Since you're already referring to the column in question by its ordinal
number within the field collection, I'd use:
tsEndTime = rdr.GetDateTime(15);
Nov 17 '05 #3
Octavio,

thank you for your answer.

When I use =(DateTime) rdr.GetValue(15), it's not always the right result.
For example: If my stored procedure returns time: '14:20:00' then this cast
will return error.
If I use convert then it returns me dateTime format.

Another example:

My stored procedure returns small int for example 0.

If I use:

int16 varI;

varI=(int16)rdr.GetValue(10) I get an error.

If I use:

varI=convert.toInt16(rdr.GetValue(10)) then it works.

So, I decided that I use everywhere convert function, unless at string data.

Do you know maybe, why this difference?

Regards,S
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S


Nov 17 '05 #4
I believe when you do a cast, it's just reinterpret the "binary value"
into the other type, so if it's invalid, it may throw an exception.

But in "Convert" operation, it probably is doing more than one case
under the covers. It could do something which cannot be casted directly.

So, I believe the Convert is more safe, while the case is more efficient.

HTH

simon wrote:
Octavio,

thank you for your answer.

When I use =(DateTime) rdr.GetValue(15), it's not always the right result.
For example: If my stored procedure returns time: '14:20:00' then this cast
will return error.
If I use convert then it returns me dateTime format.

Another example:

My stored procedure returns small int for example 0.

If I use:

int16 varI;

varI=(int16)rdr.GetValue(10) I get an error.

If I use:

varI=convert.toInt16(rdr.GetValue(10)) then it works.

So, I decided that I use everywhere convert function, unless at string data.

Do you know maybe, why this difference?

Regards,S
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15) )

What is the difference?

Thanks,S



Nov 17 '05 #5
Simon,

a) Writing rdr.GetValue(15) is the same as writing rdr[15] - at runtime,
this may be a string, a DateTime or an int, but at compile time you have an
Object, and you will need the cast (or the conversion) in order for the code
to compile.

b) If you know that the 16th field is a DateTime, you can use
rdr.GetDateTime(15) which returns directly a DateTime (no cast or conversion
necessary).

c) Does your stored procedure produce in the 16th field a SQL Server
DATETIME, or a string which represents a date (and/or a time)? If it is a
string, then the cast to (DateTime) will fail (because the object is a
string, not a DateTime); the call to Convert.ToDateTime() will succeed,
provided that the string really contains the representation of a date/time.

d) Regarding the Int16, if the SP returns a small int the following code:

Int16 varI = rdr.GetInt16(10);

should work.

In general, when using data readers I prefer to rely on the specific methods
(GetInt16, GetDateTime, etc.) whenever I know the structure of the data at
hand. Only if writing some kind of "generic" read I use GetValue():

Regarding the Convert class, I avoid using it and generally succeed on that.
It reminds me of the times of VB6 and the abuse of conversions between
types. But that's only a personal opinion.

Regards - Octavio

"simon" <si*********@iware.si> escribió en el mensaje
news:Ec*******************@news.siol.net...
Octavio,

thank you for your answer.

When I use =(DateTime) rdr.GetValue(15), it's not always the right result.
For example: If my stored procedure returns time: '14:20:00' then this
cast will return error.
If I use convert then it returns me dateTime format.

Another example:

My stored procedure returns small int for example 0.

If I use:

int16 varI;

varI=(int16)rdr.GetValue(10) I get an error.

If I use:

varI=convert.toInt16(rdr.GetValue(10)) then it works.

So, I decided that I use everywhere convert function, unless at string
data.

Do you know maybe, why this difference?

Regards,S
"Octavio Hernandez" <do****@danysoft.com> wrote in message
news:eO**************@TK2MSFTNGP14.phx.gbl...
Simon,

I think you should use:

tsEndTime = rdr.GetDateTime(15); // no cast or conversion necessary

Of the two versions you mention, the first

tsEndTime=(DateTime) rdr.GetValue(15);

is more efficient, as the cast does not incur the penalty of the
(unnecessary) call to Convert.ToDateTime in the second version.

Regards - Octavio
"simon" <si*********@iware.si> escribió en el mensaje
news:n_*******************@news.siol.net...
I have datetime variable:

Datetime tsEndTime;

Should I use (DateTime):
tsEndTime=(DateTime)rdr.GetValue(15)

or is better to use:

tsEndTime=Convert.ToDateTime(rdr.GetValue(15))

What is the difference?

Thanks,S



Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Franck | last post: by
19 posts views Thread by simon | last post: by
3 posts views Thread by sparkle | last post: by
2 posts views Thread by SimonZ | last post: by
5 posts views Thread by rsanan | last post: by
4 posts views Thread by =?Utf-8?B?YW5kcmV3?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.