检查数据表中是否包含空值的最佳方法是什么?
在我们的场景中,大多数时候,一列将全部为空值。
(此数据表由第 3 方应用程序返回 - 我们正尝试在我们的应用程序处理数据表之前进行验证)
最佳答案
尝试将列的值与 DBNull.Value
值进行比较,以您认为合适的任何方式过滤和管理空值。
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
如果要检查表中是否存在空值可以使用此方法:
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();
关于c# - 检查数据表中是否有空值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4604414/