Hi Lerp,
Your suspicions are well-founded. The code snippet you posted -
arrivalcity = rd.GetString(Convert.ToInt16(rd.GetOrdinal("arriva lcity")))
is wrong. The GetString() method of a DataReader takes an Integer as an
argument. The GetOrdinal() method returns an Integer (the ordinal position
of the column in the result set). Therefore, there is no need to convert the
Integer returned by the GetOrdinal() method to an integer, as it already IS
one.
However, it looks like you're using VB.Net and have Option Strict turned
OFF. The first thing I would advise you to do, if you have any desire to see
better performance in your app, is to turn Option Strict ON. It will take
some getting used to to strongly type everything, but it gets easier, and
prevents a lot of trouble, as well as speeding up your app's execution.
When you use strong data-typing, you can't do:
strFirst=objDR("Fname")
objDR("Fname") is of type Object, which is NOT a string.
You could do:
strFirst=CType(objDR("Fname"), String)
Also, your second example is wrong:
strFirst=objDR.GetOrdinal("Fname")
Assuming that strFirst is a string variable, the GetOrdinal() method of a
DataReader returns the ordinal position of the column in the result set, NOT
the value, and it is an integer, not a string. So, your second line of code
assigns the ordinal position of the column to a string, which is neither the
value in that column, nor a string.
There are a couple of ways to do this efficiently, and I'm not sure which is
the most efficient:
strFirst = Convert.ToInt32(objDR("Fname"))
strFirst = objDR.GetString(objDR.GetOrdinal("Fname"))
Of course, if you already know the order in which the columns appear, you
can omit the GetOrdinal() method, and simply use the ordinal position
(definitely fastest):
strFirst = objDR.GetString(0)
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Lerp" <ad***@officience.ca> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...
Hi all,
With regards to calling data from a database and filling in an editing
form based on some query, which is the best (least intensive on processor)
method for assigning the returned results to a variable.
I have been using a couple of ways so far in my project.:
1. strFirst=objDR("Fname")
2. strFirst=objDR.GetOrdinal("Fname")
The other reason I am asking is because I have encountered some code in
one of the older versions of the project I am working on and I am unsure why
someone would write it in such a manner:
1. arrivalcity =
rd.GetString(Convert.ToInt16(rd.GetOrdinal("arriva lcity")))
Why would you convert a string value into an integer and then back into a
string again?
Regards, Lerp