SQLDataReader -- more questions

D

Dom

Still teaching myself about SQLDataReader and associated classes. I
have to say, the new HELP screens from MS are just about useless!!

Let's assume I have just read some records from a table into the
SQLDataReader object, r.

1. Am I right to assume that accessing the fields as r[int] is a
little faster than r[name]?

2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

Thanks,

Dom
 
S

sloan

1. You get a small savings, because there is no lookup. But yes, you are
right.

2. No. You can use GetString, GetDateTime...and you SHOULD use
these....GetValue is just there as a last resort.

3. Since we don't know your query, r[0] and r[x] mean nothing to us. Put
you values into a temp variable...and then do the subtraction.
You might want to look up the TimeSpan objects as well.


If you want to see a maintainable, readable way to use GetString(
ordinalNumber )....check this blog entry:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry

download the code, and look at the CustomerController (or AnythingController
for that matter).
 
M

Michael Rubinstein

Dom,
2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

r.GetInt32(i), r.GetFloat(i)...

r.GetDateTime(i) is better then casting. Checking for DbNull is highly
recommended

if (!r.IsDbNull(i))
DateTime myDate = r.GetDateTime(i));
3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

System.TimeSpan tsp = r.GetDateTime(0) - r.GetDateTime(1);
//or System.TimeSpan tsp = r.GetDateTime(0).Substract(r.GeDateTime(1));

int days = tsp.Days;

Michael

Dom said:
Still teaching myself about SQLDataReader and associated classes. I
have to say, the new HELP screens from MS are just about useless!!

Let's assume I have just read some records from a table into the
SQLDataReader object, r.

1. Am I right to assume that accessing the fields as r[int] is a
little faster than r[name]?

2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

Thanks,

Dom
 
S

sloan

One caveat.

tsp.TotalDays

There is a subtle difference between .Days and .TotalDays, fyi.


http://msdn2.microsoft.com/en-us/library/system.timespan_members.aspx


Just an fyi....it depends on your need. I just mention it because
".Total_____" is at the bottom of the intellisense options, and sometimes
you don't see it.




Michael Rubinstein said:
Dom,
2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

r.GetInt32(i), r.GetFloat(i)...

r.GetDateTime(i) is better then casting. Checking for DbNull is highly
recommended

if (!r.IsDbNull(i))
DateTime myDate = r.GetDateTime(i));
3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

System.TimeSpan tsp = r.GetDateTime(0) - r.GetDateTime(1);
//or System.TimeSpan tsp = r.GetDateTime(0).Substract(r.GeDateTime(1));

int days = tsp.Days;

Michael

Dom said:
Still teaching myself about SQLDataReader and associated classes. I
have to say, the new HELP screens from MS are just about useless!!

Let's assume I have just read some records from a table into the
SQLDataReader object, r.

1. Am I right to assume that accessing the fields as r[int] is a
little faster than r[name]?

2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

Thanks,

Dom
 
M

Michael Rubinstein

Sloan, I rely more on reading help rather then intellisense, but thanks
anyway. The difference is not that subtle - TimeSpan.TotalXxx properties are
double, while TimeSpan.Days (Minutes,Seconds...) are int . Since the
original question refers to the number of days between two dates
TimeSpan.Days property is a natural choice, unless the software was ordered
by a Phone Company. A Phone Company would round up, even for a single tick.

Michael

sloan said:
One caveat.

tsp.TotalDays

There is a subtle difference between .Days and .TotalDays, fyi.


http://msdn2.microsoft.com/en-us/library/system.timespan_members.aspx


Just an fyi....it depends on your need. I just mention it because
".Total_____" is at the bottom of the intellisense options, and sometimes
you don't see it.




Michael Rubinstein said:
Dom,
2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

r.GetInt32(i), r.GetFloat(i)...

r.GetDateTime(i) is better then casting. Checking for DbNull is highly
recommended

if (!r.IsDbNull(i))
DateTime myDate = r.GetDateTime(i));
3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

System.TimeSpan tsp = r.GetDateTime(0) - r.GetDateTime(1);
//or System.TimeSpan tsp =
r.GetDateTime(0).Substract(r.GeDateTime(1));

int days = tsp.Days;

Michael

Dom said:
Still teaching myself about SQLDataReader and associated classes. I
have to say, the new HELP screens from MS are just about useless!!

Let's assume I have just read some records from a table into the
SQLDataReader object, r.

1. Am I right to assume that accessing the fields as r[int] is a
little faster than r[name]?

2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

Thanks,

Dom
 
S

sloan

Dude, I was just trying to help the OP out a little bit. Not criticize your
post.

Since obviously he (and not you) is new to the TimeSpan object, I wanted him
to see the available options.
Not just for this particuliar problem, but for the future and future needs
he hasn't come up against yet.

Thus I provided the msdn link to the members.

..........

So if I wronged you in some manner, I apologize. I was just trying to help
the OP, which I thought was the purpose of these newsgroups.




Michael Rubinstein said:
Sloan, I rely more on reading help rather then intellisense, but thanks
anyway. The difference is not that subtle - TimeSpan.TotalXxx properties
are double, while TimeSpan.Days (Minutes,Seconds...) are int . Since the
original question refers to the number of days between two dates
TimeSpan.Days property is a natural choice, unless the software was
ordered by a Phone Company. A Phone Company would round up, even for a
single tick.

Michael

sloan said:
One caveat.

tsp.TotalDays

There is a subtle difference between .Days and .TotalDays, fyi.


http://msdn2.microsoft.com/en-us/library/system.timespan_members.aspx


Just an fyi....it depends on your need. I just mention it because
".Total_____" is at the bottom of the intellisense options, and sometimes
you don't see it.




Michael Rubinstein said:
Dom,
2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

r.GetInt32(i), r.GetFloat(i)...

r.GetDateTime(i) is better then casting. Checking for DbNull is highly
recommended

if (!r.IsDbNull(i))
DateTime myDate = r.GetDateTime(i));

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

System.TimeSpan tsp = r.GetDateTime(0) - r.GetDateTime(1);
//or System.TimeSpan tsp =
r.GetDateTime(0).Substract(r.GeDateTime(1));

int days = tsp.Days;

Michael

Still teaching myself about SQLDataReader and associated classes. I
have to say, the new HELP screens from MS are just about useless!!

Let's assume I have just read some records from a table into the
SQLDataReader object, r.

1. Am I right to assume that accessing the fields as r[int] is a
little faster than r[name]?

2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

Thanks,

Dom
 
M

Michael Rubinstein

Sloan, you did me no wrong and I have pretty thick skin (or so I think
<g>).

Cheers, Michael

sloan said:
Dude, I was just trying to help the OP out a little bit. Not criticize
your post.

Since obviously he (and not you) is new to the TimeSpan object, I wanted
him to see the available options.
Not just for this particuliar problem, but for the future and future needs
he hasn't come up against yet.

Thus I provided the msdn link to the members.

.........

So if I wronged you in some manner, I apologize. I was just trying to
help the OP, which I thought was the purpose of these newsgroups.




Michael Rubinstein said:
Sloan, I rely more on reading help rather then intellisense, but
thanks anyway. The difference is not that subtle - TimeSpan.TotalXxx
properties are double, while TimeSpan.Days (Minutes,Seconds...) are int .
Since the original question refers to the number of days between two
dates TimeSpan.Days property is a natural choice, unless the software was
ordered by a Phone Company. A Phone Company would round up, even for a
single tick.

Michael

sloan said:
One caveat.

tsp.TotalDays

There is a subtle difference between .Days and .TotalDays, fyi.


http://msdn2.microsoft.com/en-us/library/system.timespan_members.aspx


Just an fyi....it depends on your need. I just mention it because
".Total_____" is at the bottom of the intellisense options, and
sometimes you don't see it.




Dom,
2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

r.GetInt32(i), r.GetFloat(i)...

r.GetDateTime(i) is better then casting. Checking for DbNull is highly
recommended

if (!r.IsDbNull(i))
DateTime myDate = r.GetDateTime(i));

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

System.TimeSpan tsp = r.GetDateTime(0) - r.GetDateTime(1);
//or System.TimeSpan tsp =
r.GetDateTime(0).Substract(r.GeDateTime(1));

int days = tsp.Days;

Michael

Still teaching myself about SQLDataReader and associated classes. I
have to say, the new HELP screens from MS are just about useless!!

Let's assume I have just read some records from a table into the
SQLDataReader object, r.

1. Am I right to assume that accessing the fields as r[int] is a
little faster than r[name]?

2. The elements of r are all objects. I can changes these to the
appropriate objects by upcasting, (string) r[0], or (DateTime) r[0],
etc. But how do I get a field that is an integer, or a float?

3. This isn't really about SQLDataReader, but it came up at the same
time. How do I get the number of days between 2 dates. I tried
((DateTime) r[0]).Subtract ((DateTime) r[1]), but it didn't work.

Thanks,

Dom
 

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