473,659 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1844

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

2. No. You can use GetString, GetDateTime...a nd you SHOULD use
these....GetVal ue 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.space s.live.com/Blog/cns!A68482B9628 A842A!140.entry

download the code, and look at the CustomerControl ler (or AnythingControl ler
for that matter).


"Dom" <do********@gma il.comwrote in message
news:28******** *************** ***********@v4g 2000hsf.googleg roups.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.G eDateTime(1));

int days = tsp.Days;

Michael

"Dom" <do********@gma il.comwrote in message
news:28******** *************** ***********@v4g 2000hsf.googleg roups.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.com wrote in message
news:5s******** *****@mid.indiv idual.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.G eDateTime(1));

int days = tsp.Days;

Michael

"Dom" <do********@gma il.comwrote in message
news:28******** *************** ***********@v4g 2000hsf.googleg roups.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
SQLDataReade r 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
536
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
2358
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: ************************************************* public void Export2CSV(){
3
5880
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 combo's text property (what the user will see). Is this possible with a SqlDataReader? Thanks
5
12127
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 output (eg. delete a row in the datagrid) and update the change in the corresponding table in the database? thanks for your time.
1
3254
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 sqldatareader then sqldatareader.nextresult
4
2359
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 declare it outside try scope either since we have to call cmd.executeReader to create sqldatareader public string GetLogs(int logID) {
1
1359
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 out dated?
2
516
by: Daniel | last post by:
is there any limit to how long of a string SqlDataReader.GetString() can return?
3
4191
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 object. It seems I can only move through it sequentially. Is that true? Dom
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.