我有以下代码
foreach (DataRowView dr in Data)
{
if (dr == System.DBNull.Value)
{
nedID = 1;
}
}
但是我收到以下错误
运算符==
无法应用于System.Data.DataRowView
和System.DBNull
类型的操作数
请有人建议我如何检查值是否为空或DBNULL
答案 0 :(得分:13)
您需要指定字段名称或索引。
foreach (DataRowView dr in Data)
{
if (dr["nameOfField"] == System.DBNull.Value)
{
nedID = 1;
}
}
答案 1 :(得分:5)
您需要将dr == System.DBNull.Value
替换为...
Convert.IsDBNull(dr["somefield"])
如果是DBNnull
则返回true