Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.Net: Check if field name exists on IDataRecord

Tags:

c#

ado.net

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);
}
like image 997
ArBR Avatar asked Nov 08 '10 17:11

ArBR


People also ask

How do I get the DataReader of an idatarecord?

Typically, you do this by obtaining a DataReader through the ExecuteReader method of the Command object. Classes that inherit IDataRecord must implement all inherited members, and typically define additional members to add provider-specific functionality.

How do I create an instance of the idatarecord interface?

An application does not create an instance of the IDataRecord interface directly, but creates an instance of a class that inherits IDataRecord. Typically, you do this by obtaining a DataReader through the ExecuteReader method of the Command object.

How to check if a field exists or not in SQL?

You will want to check for it in Sql. IF EXISTS ( SELECT * FROM table WHERE field = value) BEGIN -- do an update since it exists UPDATE table SET field1 = value. field2 = value2 WHERE field = value END ELSE BEGIN -- Since it does not exist, do an insert INSERT INTO ... END The content must be between 30 and 50000 characters.


1 Answers

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.

like image 70
Guffa Avatar answered Sep 30 '22 12:09

Guffa