ADO.Net SqlDataReader - GetOrdinal via static variables.

  • Thread starter Thread starter Jay Allard
  • Start date Start date
J

Jay Allard

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(sDataView);
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();
}
 
That's a fine approach, so is using an Enum which Bill Vaughn first
mentioned in his best practices book.
 
Back
Top