从IDataReader获取值的空安全方法

时间:2010-04-09 18:26:42

标签: c# extension-methods dbnull idatareader

(LocalVariable)ABC.string(Name)=(Idatareader)datareader.GetString(0);

此名称值来自数据库.. 这里发生的事情是,如果这个名称值为null而读取它会引发异常吗?

我在这里手动做一些条件。我不想写一个手动条件来检查我的所有变量..

我现在正在做这样的事情..

string abc =  (Idatareader)datareader.GetValue(0);
if(abc = null)
    //assiging null
else
    assiging abc value

是否可以为此编写扩展方法? 感谢

5 个答案:

答案 0 :(得分:22)

这里有几个扩展方法,可以很好地结束从数据读取器中检索强类型值的所有问题。如果值为DbNull,则将返回该类型的默认值。如果string是一个类,则会返回null。如果该字段为int,则会返回0。另外,如果您期望int?,例如来自可空int字段,则会返回null

Kumar案件的具体用法:

string abc = datareader.GetValueOrDefault<string>(0);

一般用法

var name = GetValueOrDefault<string>(reader, "Name");

var name = reader.GetValueOrDefault<string>("Name");

var name = reader.GetValueOrDefault<string>(0);

扩展

public static class NullSafeGetter
{
   public static T GetValueOrDefault<T>(this IDataRecord row, string fieldName)
   {
       int ordinal = row.GetOrdinal(fieldName);
       return row.GetValueOrDefault<T>(ordinal);
   }

   public static T GetValueOrDefault<T>(this IDataRecord row, int ordinal)
   {
       return (T)(row.IsDBNull(ordinal) ? default(T) : row.GetValue(ordinal));
   }
}

来自http://skysanders.net/subtext/archive/2010/03/02/generic-nullsafe-idatarecord-field-getter.aspx

答案 1 :(得分:3)

类似于@ sky-sanders的答案,但对转换不太严格:

public static T Get<T>(this IDataRecord row, string fieldName)
{
    int ordinal = row.GetOrdinal(fieldName);
    return row.Get<T>(ordinal);
}

public static T Get<T>(this IDataRecord row, int ordinal)
{
    var value = row.IsDBNull(ordinal) ? default(T) : row.GetValue(ordinal);
    return (T)Convert.ChangeType(value, typeof(T));
}

答案 2 :(得分:2)

我的解决方案是:

private static T GetValue<T>(object o) {
    if (typeof(DBNull) != o.GetType()) {
        return (T) o;
    }
    return default(T);
}

何时,Status = GetValue<string>(currentDataRow["status"])

答案 3 :(得分:2)

结合顶级解决方案和建议,这是一个 C#6箭头表达式版本,支持GetValue<T>GetValueOrDefault<T>以及可选的默认值参数。

public static class DataRecordExtensions {
    /// <summary>
    /// Generically extracts a field value by name from any IDataRecord as specified type. Will throw if DNE.
    /// </summary>
    public static T GetValue<T>(this IDataRecord row, string fieldName)
        => row.GetValue<T>(row.GetOrdinal(fieldName));

    /// <summary>
    /// Generically extracts a field value by ordinal from any IDataRecord as specified type. Will throw if DNE.
    /// </summary>
    public static T GetValue<T>(this IDataRecord row, int ordinal)
        => (T)row.GetValue(ordinal);

    /// <summary>
    /// Generically extracts a field value by name from any IDataRecord as specified type. Will return default generic types value if DNE.
    /// </summary>
    public static T GetValueOrDefault<T>(this IDataRecord row, string fieldName, T defaultValue = default(T))
        => row.GetValueOrDefault<T>(row.GetOrdinal(fieldName), defaultValue);

    /// <summary>
    /// Generically extracts a field value by ordinal from any IDataRecord as specified type. Will return default generic types value if DNE.
    /// </summary>
    public static T GetValueOrDefault<T>(this IDataRecord row, int ordinal, T defaultValue = default(T))
        => (T)(row.IsDBNull(ordinal) ? defaultValue : row.GetValue(ordinal));
}

答案 4 :(得分:0)

我会用这样的东西:

string abc = (IDataReader)datareader.GetValue(0) ?? "Default";