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
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").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With