473,324 Members | 2,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

SQLDataReader -- more questions

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
Dec 12 '07 #1
3 1826

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).


"Dom" <do********@gmail.comwrote in message
news:28**********************************@v4g2000h sf.googlegroups.com...
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

Dec 12 '07 #2
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" <do********@gmail.comwrote in message
news:28**********************************@v4g2000h sf.googlegroups.com...
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

Dec 12 '07 #3

One caveat.

tsp.TotalDays

There is a subtle difference between .Days and .TotalDays, fyi.
http://msdn2.microsoft.com/en-us/lib...n_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" <mSPAM_REMOVEr@m®ubinstein.comwrote in message
news:5s*************@mid.individual.net...
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" <do********@gmail.comwrote in message
news:28**********************************@v4g2000h sf.googlegroups.com...
>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


Dec 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
5
by: Wing | last post by:
Hi all, I execute a stored procedure in my C# code, assign the result to the SqlDataReader object and display it with datagrid. the question I like to ask, is possible to edit the datagrid...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
1
by: Daniel | last post by:
for some reason when i deploy my C# application on windows 2003 occasionaly rdr.ToString() hangs where rdr is a System.Data.SqlClient.SqlDataReader. Is there a fix for this? is my visual studio.net...
2
by: Daniel | last post by:
is there any limit to how long of a string SqlDataReader.GetString() can return?
3
by: Dom | last post by:
In the old days, using ADO, I used to be able to move up and down in a resultset, but using functions like MoveNext, MoveFirst, MoveLast, MovePrev. I don't see any of that in the SQLDataReader...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.