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

which is best method?

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


Nov 18 '05 #1
4 1769
Chances are if you are running into performance issues, you won't solve them
for by tweaking this code. I also think you are missing what GetOrdinal
does...

The fastest thing to do is (why call it objDR?, everything's an object)
dr.GetInt32(0) which is referencing your column my #. Now, that might be
the fastest, but it's also the worst (poor readability, high likelyhood of
breaking).

dr("Fname") is the slowest.

GetOrdinal returns an integer...so strFirst = objDR("Fname") doesn't make
much sense. It returns the column # of the named field, so once you have
that integer, you can use the fast method above without the drawbacks:

dim firstNameColumnId as integer = dr.GetOrdinale("Fname")
while dr.read()
dim firstName as string = dr.GetString(firstNameColumnId)
end while
You also misunderstood what the code you once saw does. Except the person
who wrote it clearly thought they were writing very performant code, but
their cleverness hurts performance more than it helps.

rd.GetOrdinal("arrivalcity") returns an integer (Int32). For some
misguided reason they are converting it to Int16 (which is a performance
hit). rd.GetString() expects the column #, which they are getting from
GetOrdinal and returns a string..so all good there.

The problem with the code, other than the int16 conversion, is that the
entire point of GetOrdinal is that it does the lookup once...so you can
store the column # in a variable and use it thereafter. The code doesn't do
that...If they do a dr.read(), they'll lose the ordinal value of the
arrivalcity column and have to do it again...

Karl
"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

Nov 18 '05 #2
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

Nov 18 '05 #3
*shaking his fist at kevin*

"Kevin Spencer" <ks******@takempis.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
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


Nov 18 '05 #4
Hi guys,

Thank you very much... i figured that code was a little off :). I will
stick to option strict and use the ordinal example (strFirst =
objDR.GetString(0)) Kevin specified in his reply. I really appreciate your
help guys thank you very much. Now to update this version :) lolol

Cheers, Lerp :)


"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

Nov 18 '05 #5

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

Similar topics

0
by: Laphan | last post by:
I know this is a crosspost, but I need to know which is the best one for posting INET FTP queries, so that I'm not cross-posting in future. Now that I've posted could somebody let me know: 1)...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
3
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following...
3
by: Ashish Kanoongo | last post by:
Let me know the best authentication method and why ? I am asking in ASP.NET using C#. 1. Windows Builtin authentication 2. Passport base authentication 3. Form Based Authentication 4. Default...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
11
by: Farel | last post by:
Which is Faster in Python and Why? jc = {}; m = x = ,,.......] # upwards of 10000 entries def mcountb(): for item in x: b = item; b.sort(); bc = 0 for bitem in b: bc += int(bitem) try: m...
7
by: Gladen Blackshield | last post by:
Hello All! Still very new to PHP and I was wondering about the easiest and simplest way to go about doing something for a project I am working on. I would simply like advice on what I'm asking...
1
by: esd | last post by:
Hello folks, the two code blocks below do the same thing, which is set a few form field values. Clearly the first method is the most elegant but my question is which is best in terms of lightest...
3
by: d d | last post by:
I have a large object with many sub-objects. They may go down 4 or 5 levels and I'm not sure how best to code a method that does operations on that data. Here's an attempt to make it an isolated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...

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.