473,396 Members | 2,055 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.

ADO.Net SqlDataReader - GetOrdinal via static variables.

Greetings

When looping through a SQL Data Reader, it seems that it would be
inefficient to call GetOrdinal(fieldname) multiple times. So, I
thought I'd assign them to some variables before beginning the loop,
that way it only happens once per method call. Then I thought I'd make
them static so that it only has to do it once ever.

I'd like to know if this is a good approach, or not. If not, please
tell me why. Are there better ways to do this? Am I making a big deal
out of nothing?
private static int F_TABLE_NAME = -1;
private static int F_COLUMN_NAME = -1;
private static int F_DESCRIPTION = -1;
private static int F_FRIENDLY_NAME = -1;
private static int F_HAS_DESCRIPTION = -1;
private static int F_CATEGORY = -1;
private static int F_IS_EXTENDED = -1;
private void DrawAvailable()
{
SqlDataReader dr = TableExtensionManager.GetExtendedTableFieldsDR(sDa taView);
if (F_TABLE_NAME == -1)
{
F_TABLE_NAME = dr.GetOrdinal("TableName");
F_COLUMN_NAME = dr.GetOrdinal("ColumnName");
F_DESCRIPTION = dr.GetOrdinal("Description");
F_FRIENDLY_NAME = dr.GetOrdinal("FriendlyName");
F_HAS_DESCRIPTION = dr.GetOrdinal("HasDescription");
F_CATEGORY = dr.GetOrdinal("Category");
F_IS_EXTENDED = dr.GetOrdinal("IsExtended");
}
while (dr.Read())
{
string x = dr.GetString(F_DESCRIPTION);
//do something with x
}
dr.Close();
}
Nov 15 '05 #1
1 3469
That's a fine approach, so is using an Enum which Bill Vaughn first
mentioned in his best practices book.
"Jay Allard" <ja*@allardworks.com> wrote in message
news:46*************************@posting.google.co m...
Greetings

When looping through a SQL Data Reader, it seems that it would be
inefficient to call GetOrdinal(fieldname) multiple times. So, I
thought I'd assign them to some variables before beginning the loop,
that way it only happens once per method call. Then I thought I'd make
them static so that it only has to do it once ever.

I'd like to know if this is a good approach, or not. If not, please
tell me why. Are there better ways to do this? Am I making a big deal
out of nothing?
private static int F_TABLE_NAME = -1;
private static int F_COLUMN_NAME = -1;
private static int F_DESCRIPTION = -1;
private static int F_FRIENDLY_NAME = -1;
private static int F_HAS_DESCRIPTION = -1;
private static int F_CATEGORY = -1;
private static int F_IS_EXTENDED = -1;
private void DrawAvailable()
{
SqlDataReader dr = TableExtensionManager.GetExtendedTableFieldsDR(sDa taView); if (F_TABLE_NAME == -1)
{
F_TABLE_NAME = dr.GetOrdinal("TableName");
F_COLUMN_NAME = dr.GetOrdinal("ColumnName");
F_DESCRIPTION = dr.GetOrdinal("Description");
F_FRIENDLY_NAME = dr.GetOrdinal("FriendlyName");
F_HAS_DESCRIPTION = dr.GetOrdinal("HasDescription");
F_CATEGORY = dr.GetOrdinal("Category");
F_IS_EXTENDED = dr.GetOrdinal("IsExtended");
}
while (dr.Read())
{
string x = dr.GetString(F_DESCRIPTION);
//do something with x
}
dr.Close();
}

Nov 15 '05 #2

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

Similar topics

2
by: Rod | last post by:
Thanks in advance. I have two listboxes on a single asp page. I am trying to use sqldatareader to populate both using two seperate sql stored procs. I can populate each box seperately using...
3
by: Smith Simson | last post by:
I am reading the data from database using SqlDataReader. Here is my code..... txtCustomerName = MyDataReader.GetString(MyDataReader.GetOrdinal("CustomerName")) txtCustomerDOB =...
2
by: Cameron Frasnelly | last post by:
I emulated the code from the .Net Framework help (Titled "Using Stored Procedures with a Command") and I still receive and error... Error Received = "Invalid attempt to read when no data is...
5
by: Tom Edelbrok | last post by:
I notice that using the SqlDataReader requires the use of ordinal field references rather than by name. For example, do while (myDataReader.Read())...
7
by: Web learner | last post by:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows: The name 'dr' does not exist in the current...
13
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the...
3
by: Frank Milverckowitz | last post by:
Hi, Newbie question about SqlDataReader column value access... In java jdbc code to get a value from a table column we can pass the column name (instead of an int index or offset): String...
4
by: Prem | last post by:
This is what I am trying to do double utmx = (double) (pSqlReader.GetValue(pSqlReader.GetOrdinal("UTM_X83"))); and this is the exception that I get System.InvalidCastException was unhandled...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.