加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 单个方法,可以返回数据行中列的值,并自动处理列值为null的

发布时间:2020-12-15 17:25:29 所属栏目:百科 来源:网络整理
导读:任何人都可以建议一个方法,它可以返回数据行中列的值,并自动处理列值为null的可能性.基本上我试图想出一个利用DataRow扩展方法来处理DBNull值的通用解决方案.我的解决方案到现在为止: public static NullableT SafeReadT(DataRow row,string fieldName) whe
任何人都可以建议一个方法,它可以返回数据行中列的值,并自动处理列值为null的可能性.基本上我试图想出一个利用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#不允许基于参数约束的方法重载

解决方法

以下内容将为您提供数据行中的值,如果值为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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读