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 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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ricola ! |
last post by:
Why do I say:
SqlDataReader dr;
instead of
SqlDataReader dr = new SqlDataReader();
|
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(){
|
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
|
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.
|
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
| |
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)
{
|
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?
|
by: Daniel |
last post by:
is there any limit to how long of a string SqlDataReader.GetString() can
return?
|
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
|
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,...
|
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...
| |
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |