最好的方法来检查,如果数据表中有一个空值 [英] Best way to check if a Data Table has a null value in it

查看:484
本文介绍了最好的方法来检查,如果数据表中有一个空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是检查数据表中有一个空值的最佳方式是什么?

what is the best way to check if a Data Table has a null value in it ?

大多数在我们的场景时,一列将所有空值。

Most of the time in our scenario, one column will have all null values.

(该数据表是由一个第三方应用程序返回 - 我们正试图把一个valiadation之前,我们的应用程序处理的数据表)

(This datatable is returned by a 3rd party application - we are trying to put a valiadation before our application processes the data-table)

推荐答案

尝试比较列于的DBNull.Value 值的值过滤和处理空值无论你认为合适的。

Try comparing the value of the column to the DBNull.Value value to filter and manage null values in whatever way you see fit.

foreach(DataRow row in table.Rows)
{
    object value = row["ColumnName"];
    if (value == DBNull.Value)
        // do something
    else
        // do something else
}

关于DBNull类更多信息

如果您要检查是否为空值的表存在,你可以使用这个方法:

If you want to check if a null value exists in the table you can use this method:

public static bool HasNull(this DataTable table)
{
    foreach (DataColumn column in table.Columns)
    {
        if (table.Rows.OfType<DataRow>().Any(r => r.IsNull(column)))
            return true;
    }

    return false;
}

这将让你这样写:

which will let you write this:

table.HasNull();

这篇关于最好的方法来检查,如果数据表中有一个空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆