I have two cases to extract information from the IDataReader object
Case – 1 – Length, calculates ordinal and then parse string
public static string GetString(IDataReader rdr, string columnName)
{
int ordinal = rdr.GetOrdinal(columnName);
if (rdr.IsDBNull(ordinal))
{
return string.Empty;
}
return (string)rdr[ordinal];
}
Case – 2, short way, getting data without calculating ordinal
public static string GetString(IDataReader rdr, string columnName)
{
return (string)rdr[columnName];
}
Which technique should be preferred and why and if any specific context ?
SqlDataReader’s
this[string name]looks like:so it calculates ordinal internaly, and there is no difference what way to use.
UPDATE
Yo can rewrite your code as: