4

任何人都可以建议一种可以返回数据行中列的值并自动处理列值为空的可能性的方法。本质上,我试图提出一个通用的解决方案,利用 DataRow 扩展方法来处理 DBNull 值。到目前为止,我的解决方案是:

public static Nullable<T> SafeRead<T>(DataRow row, string fieldName) where T : struct
{
    if (row.HasColumn(fieldName))
    {
        return row.Field<Nullable<T>>(fieldName) ?? default(Nullable<T>);
    }
    else
        return default(Nullable<T>);
}

public static T SafeRead<T>(DataRow row, string fieldName) where T : class
{
    if (row.HasColumn(fieldName))
    {
        return row.Field<T>(fieldName) ?? default(T);
    }
    else
        return default(T);
}

但这显然抱怨方法歧义,因为 C# 不允许基于参数约束的方法重载

4

2 回答 2

2

以下将为您提供从数据行返回的值,如果值为 DBNull.Value,则为该类型的默认值。如果未定义该字段,则会抛出 ArgumentException。

using System;
using System.Data;

public static class DataAccess
{
    public static T GetValueOrDefault<T>(DataRow row, string fieldName)
    {
        if (!row.Table.Columns.Contains(fieldName))
        {
            throw new ArgumentException(
                string.Format("The given DataRow does not contain a field with the name \"{0}\".", fieldName));
        }
        else if (row[fieldName].Equals(DBNull.Value))
        {
            return default(T);
        }

        return row.Field<T>(fieldName);
    }
}

这里有一些简单的测试:

[TestMethod]
public void GetValueOrDefault_ValueType_Test()
{
    const string FieldName = "Column";
    const int Expected = 5;

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(int));

    DataRow row = dataTable.Rows.Add(Expected);

    int actual = DataAccess.GetValueOrDefault<int>(row, FieldName);
    Assert.AreEqual(Expected, actual);
}

[TestMethod]
public void GetValueOrDefault_ValueType_DBNull_Test()
{
    const string FieldName = "Column";

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(int));

    DataRow row = dataTable.Rows.Add(DBNull.Value);

    int actual = DataAccess.GetValueOrDefault<int>(row, FieldName);
    Assert.AreEqual(default(int), actual);
}

[TestMethod]
public void GetValueOrDefault_ReferenceType_Test()
{
    const string FieldName = "Column";
    object expected = new object();

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(object));

    DataRow row = dataTable.Rows.Add(expected);

    object actual = DataAccess.GetValueOrDefault<object>(row, FieldName);
    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void GetValueOrDefault_ReferenceType_DBNull_Test()
{
    const string FieldName = "Column";

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(FieldName, typeof(object));

    DataRow row = dataTable.Rows.Add(DBNull.Value);

    object actual = DataAccess.GetValueOrDefault<object>(row, FieldName);
    Assert.AreEqual(default(object), actual);
}
于 2012-04-18T18:00:48.993 回答
0

您总是可以自己实现 Nullable 的混蛋实现 - 并从您的函数中返回它。

public class SafeValue<T>
{
    public T Value {get; set;}
    public bool HasValue
    {
        get
        {
            if(typeof(T).IsClass)
                return Value != null;
            else return true;
        }
    }
}

因此,将两者都替换为:

public static SafeValue<T> SafeRead<T>(DataRow row, string fieldName)
{
    if (row.HasColumn(fieldName))
    {
        return new SafeValue<T>{ Value = row.Field<T>(fieldName)};
    }
    else
        return new SafeValue<T>();
}

和用法:

var val = SafeRead(row, "wah");

if(val.HasValue)
    //DoStuff();
于 2012-04-18T17:06:24.697 回答