473,386 Members | 1,668 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,386 software developers and data experts.

DataReader GetInt32

When reading from a data reader I have been using code like this :

if (objDataReader.Read() == true)
{
intNewStatus =
Convert.ToInt32(objDataReader["STATUS"]);
}

But I've seen some code examples using GetInt32 (or GetString etc). Is
there any difference between using these 2 methods, and which one is
recommended?

Thanks,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 6940
mike parr <mp********@yahoo.co.uk> wrote:
When reading from a data reader I have been using code like this :

if (objDataReader.Read() == true)
{
intNewStatus =
Convert.ToInt32(objDataReader["STATUS"]);
}

But I've seen some code examples using GetInt32 (or GetString etc). Is
there any difference between using these 2 methods, and which one is
recommended?


Personally I'd use GetInt32() etc - it makes your code clearer, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
It's probably obvious to everyone, but one subtle difference in actual
functionality is that the GetInt32() demands that the native type is already
an integer, while the other will be happy to convert from a double, for
example. One might argue that using the more strict option would be a way
to ensure a higher degree of integrity (the database type is still what you
expected).

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
mike parr <mp********@yahoo.co.uk> wrote:
When reading from a data reader I have been using code like this :

if (objDataReader.Read() == true)
{
intNewStatus =
Convert.ToInt32(objDataReader["STATUS"]);
}

But I've seen some code examples using GetInt32 (or GetString etc). Is
there any difference between using these 2 methods, and which one is
recommended?


Personally I'd use GetInt32() etc - it makes your code clearer, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Mike, as an aside, using the nominal reference "STATUS" as opposed the an
index (if status was the first field in your query, using [0] instead) can
have some performance implications. If you are doing it for one field, it
will probably be trivial, but it's still wasteful in the the Named column's
index needs resolved at every pass even though it only takes one time to
figure it out.

there are two cool ways (other than just coding the indexes which hampers
readability quite a bit unless you really comment it well).

You can delcare an intere and use the dataReader's .GetOrdinal Method. So
the int variables has a clear name (like the actual column name) and now you
can use Status instead of "Status" is you delcared an int Status and used
the GetOrdingal mehtod. Just do this outside of the read loop.

You can also create an Enum (Bill Vaughn's idea) which circumvents the need
to even use GetOrdinal while getting you to the same end result.

HTH,

Bill
"mike parr" <mp********@yahoo.co.uk> wrote in message
news:#C**************@TK2MSFTNGP12.phx.gbl...
When reading from a data reader I have been using code like this :

if (objDataReader.Read() == true)
{
intNewStatus =
Convert.ToInt32(objDataReader["STATUS"]);
}

But I've seen some code examples using GetInt32 (or GetString etc). Is
there any difference between using these 2 methods, and which one is
recommended?

Thanks,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4

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

Similar topics

2
by: Mike | last post by:
How do I select a number field from oracle 9i? The field is a number(integer). I have this line in my datareader: double dbid; dbid = dr.GetDecimal(0); I have tried getstring, convert and...
3
by: lakshmi | last post by:
Hi I'm using a SqlDataReader to read results from a SQL view that returns 10 columns. Is there anyway I can reader the columns based on their names rather than their position? strQuery =...
3
by: David | last post by:
I need to assign data from a data reader to a variable. here is a simpiet of code, OleDbDataReader dtrBMail = cmd.ExecuteReader(); if (dtrBMail.Read()){ int t_mob_var_send_no;...
6
by: Nakhi | last post by:
Hi, my code is very simple. dim cm as oledbcommand= new oledbcommand("select * from page " ,cn) cn.open() Dim dr As OleDbDataReader = cm.ExecuteReader() dim cate=dr.getstring(1) 'error line"...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
2
by: Mattyw | last post by:
Hi I have a sqlcommand that returns all the rows in a column and then pass that to a datareader. I am new to VS.Net and so far I can only return the first row in the first column using ...
5
by: jimmy | last post by:
Hi, Does anyone know how to read the contents of a datareader into an array? The datareader will only hold one field called CuttingID. Can anyone help me including how i would declare the array...
4
by: rikleo2001 | last post by:
Hello Experts. I need your help, I am new to XML web service, and I have to complete one task. Here is current situation. 1. I have web service running to recieve SOAP message from an...
3
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, How can I get value of a SQL column defined as int. I try int myInt = dataReader.GetInt32(0); I get invalid cast exception and same for GetInt16...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.