Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET - What is best practice for getting datareader values?

Tags:

.net

ado

Just wondering what is best practice for getting the values of a datareader. For example:

I have been doing this:

MyValue = Dr("ColumnName")

But have noticed other people doing:

MyValue = Dr.GetString("ColumnName")

I am intested to know the benifits of the 2nd method

like image 687
Sean Taylor Avatar asked Dec 23 '22 12:12

Sean Taylor


1 Answers

The DbDataReader.GetString(int) method can only be used if you know the index of the column. Using DbDataReader.GetString("ColumnName") is not possible as there is no such overload. I.e. you have the following two options:

 string myValue1 = (string) Dr["ColumnName"];
 string myValue2 = Dr.GetString(columIndex);

The first line internally calls DbDataReader.GetOrdinal("ColumnName").

like image 109
Jakob Christensen Avatar answered Mar 15 '23 06:03

Jakob Christensen