ArBR ArBR - 2 months ago 15
C# Question

ADO.Net: Check if field name exists on IDataRecord

Is there a better way for getting the field_name value from a IDataRecord only if the field_name exists in the IDataRecord, currently I'm using a try{...} catch{...} block, but this is some kind of On Error Resume next. Some alternatives?

/// <summary>
/// Returns column value from IDataRecord only if field_name exists.
/// </summary>
public static Tresult ValueIfExists<Tresult>(this IDataRecord record, string field_name)
{
try { return record.Value<Tresult>(record.GetOrdinal(field_name)); }
catch { return default(Tresult); }
}

/// <summary>
/// Returns column value from IDataRecord accecing by index.
/// </summary>
public static Tresult Value<Tresult>(this IDataRecord record, int field_index)
{
return record.IsDBNull(field_index) ? default(Tresult) :
(Tresult)Convert.ChangeType(record[field_index], typeof(Tresult));
}


I have changed my ValueIfExists function to reflect your ideas, so it looks like this:

public static Tresult ValueIfExists2<Tresult>(this IDataRecord record, string field_name)
{
for (int index = 0; index < record.FieldCount; index++)
{
if (record.GetName(index).Equals(field_name, StringComparison.InvariantCulture))
{
return record.Value<Tresult>(record.GetOrdinal(field_name));
}
}
return default(Tresult);
}

Answer

You are right that exceptions should not be used for normal program flow.

The GetOrdinal method is intended for situations where you know what fields you get, and if a field is missing that is an error that should result in an exception.

If you don't know which fields you get in the result, you should avoid the GetOrdinal method. You can instead get all the names and their index into a dictionary that you can use as replacement for the GetOrdinal method:

public static Dictionary<string, int> GetAllNames(this IDataRecord record) {
  var result = new Dictionary<string, int>();
  for (int i = 0; i < record.FieldCount; i++) {
    result.Add(record.GetName(i), i);
  }
  return result;
}

You can use the ContainsKey method to check if the name exists in the dictionary, or the TryGetValue method to check if the name exists and get it's index it does in a single operation.

The GetOrdinal method first does a case sensetive search for the name, and if that fails it does a case insensetive search. That is not provided by the dictionary, so if you want that exact behaviour you would rather store the names in an array and write a method to loop through them when you want to find the index.