检查数据表中是否有空值的最佳方法

发布于 2024-10-10 07:36:58 字数 110 浏览 3 评论 0原文

检查数据表中是否有空值的最佳方法是什么?

在我们的场景中,大多数情况下,一列将全部包含空值。

(此数据表由第三方应用程序返回 - 我们试图在应用程序处理数据表之前进行验证)

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你与昨日 2024-10-17 07:36:58

尝试将该列的值与 DBNull.Value 值进行比较,以您认为合适的任何方式过滤和管理空值。

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

DBNull 类的更多信息


有关 表中存在 null 值,您可以使用此方法:

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;
}

这将让您编写以下内容:

table.HasNull();

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
}

More information about the DBNull class


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();
两个我 2024-10-17 07:36:58
foreach(DataRow row in dataTable.Rows)
{
    if(row.IsNull("myColumn"))
        throw new Exception("Empty value!")
}
foreach(DataRow row in dataTable.Rows)
{
    if(row.IsNull("myColumn"))
        throw new Exception("Empty value!")
}
晚风撩人 2024-10-17 07:36:58

您可以循环抛出行和列,检查空值,跟踪布尔值是否为空值,然后在循环遍历表后检查它并处理它。

//your DataTable, replace with table get code
DataTable table = new DataTable();
bool tableHasNull = false;

foreach (DataRow row in table.Rows)
{
    foreach (DataColumn col in table.Columns)
    {
        //test for null here
        if (row[col] == DBNull.Value)
        {
            tableHasNull = true;
        }
    }
}

if (tableHasNull)
{
    //handle null in table
}

您还可以使用break 语句退出foreach 循环,例如,

//test for null here
if (row[col] == DBNull.Value)
{
    tableHasNull = true;
    break;
}

以保存对表其余部分的循环。

You can loop throw the rows and columns, checking for nulls, keeping track of whether there's a null with a bool, then check it after looping through the table and handle it.

//your DataTable, replace with table get code
DataTable table = new DataTable();
bool tableHasNull = false;

foreach (DataRow row in table.Rows)
{
    foreach (DataColumn col in table.Columns)
    {
        //test for null here
        if (row[col] == DBNull.Value)
        {
            tableHasNull = true;
        }
    }
}

if (tableHasNull)
{
    //handle null in table
}

You can also come out of the foreach loop with a break statement e.g.

//test for null here
if (row[col] == DBNull.Value)
{
    tableHasNull = true;
    break;
}

To save looping through the rest of the table.

往事随风而去 2024-10-17 07:36:58
DataTable dt = new DataTable();
foreach (DataRow dr in dt.Rows)
{
    if (dr["Column_Name"] == DBNull.Value)
    {
        //Do something
    }
    else
    {
        //Do something
    }
}
DataTable dt = new DataTable();
foreach (DataRow dr in dt.Rows)
{
    if (dr["Column_Name"] == DBNull.Value)
    {
        //Do something
    }
    else
    {
        //Do something
    }
}
时光病人 2024-10-17 07:36:58

我会像....

(!DBNull.Value.Equals(dataSet.Tables[6].Rows[0]["_id"]))

I will do like....

(!DBNull.Value.Equals(dataSet.Tables[6].Rows[0]["_id"]))
差↓一点笑了 2024-10-17 07:36:58
public static class DataRowExtensions
{
    public static T GetValue<T>(this DataRow row, string fieldName)
    {
        if (row.IsNull(fieldName))
        {
            return default(T);
        }

        var value = row[fieldName];
        if (value == DBNull.Value)
        {
            return default(T);
        }

        if (typeof(T) == typeof(string))
        {
            return (T)Convert.ChangeType(value.ToString(), typeof(T));
        }

        return (T)Convert.ChangeType((T)value, typeof(T));
    }
}

用法:

string value = row.GetValue<string>("ColumnName");
public static class DataRowExtensions
{
    public static T GetValue<T>(this DataRow row, string fieldName)
    {
        if (row.IsNull(fieldName))
        {
            return default(T);
        }

        var value = row[fieldName];
        if (value == DBNull.Value)
        {
            return default(T);
        }

        if (typeof(T) == typeof(string))
        {
            return (T)Convert.ChangeType(value.ToString(), typeof(T));
        }

        return (T)Convert.ChangeType((T)value, typeof(T));
    }
}

Usage:

string value = row.GetValue<string>("ColumnName");
你的呼吸 2024-10-17 07:36:58

您可以使用 LinQ 为空/空白/空格等值
在此处使用以下查询

   var BlankValueRows = (from dr1 in Dt.AsEnumerable()
                                  where dr1["Columnname"].ToString() == ""
                                  || dr1["Columnname"].ToString() == ""
                                   || dr1["Columnname"].ToString() == ""
                                  select Columnname);

Columnname 替换为表列名称,并将 "" 替换为上面代码中我们查找空值的搜索项。

You can null/blank/space Etc value using LinQ
Use Following Query

   var BlankValueRows = (from dr1 in Dt.AsEnumerable()
                                  where dr1["Columnname"].ToString() == ""
                                  || dr1["Columnname"].ToString() == ""
                                   || dr1["Columnname"].ToString() == ""
                                  select Columnname);

Here Replace Columnname with table column name and "" your search item in above code we looking null value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文