在数据集中查找空值 - DataRow.IsNull方法vs == DbNull.Value - c#

时间:2011-04-08 18:38:06

标签: c#-4.0 dataset datarow dbnull isnull

使用c#方法DataRow.IsNull确定空值是否等于DbNull.value会有什么好处?

if(ds.Tables[0].Rows[0].IsNull("ROWNAME")) {do stuff}

VS

if(ds.Tables[0].Rows[0]["ROWNAME"] == DbNull.value) {do stuff}

5 个答案:

答案 0 :(得分:22)

没有真正的实用好处。使用对你来说更具可读性的任何一个。

至于它们之间的特殊差异,基本答案是IsNull查询列中特定记录的空状态。使用== DBNull.Value实际检索值并在它实际为null的情况下进行替换。换句话说,IsNull检查状态而不实际检索值,因此稍微快一些(理论上至少)。

理论上,如果您要使用自定义存储类型,列可以返回其他而不是DBNull.Value的空值,但这从未完成(根据我的经验) 。如果是这种情况,IsNull将处理存储类型使用DBNull.Value以外的其他内容的情况,但是,我再也没有看到过这种情况。

答案 1 :(得分:8)

DBNull.Value != null

DBNull.Value代表值<NULL>的列。 弹出一个表并返回一些行,查看任何行中的任何列是否包含<NULL>(ctrl 0)值。如果你看到一个等同于DBNull.Value。

如果将值设置为null或DBNull.Value,则需要使用IsNull()。 如果值为null或DBNull.Value,则返回true。请考虑以下事项:

row["myCol"] = null;

row["myCol"] = DBNull.Value

if (row["myCol"] == DBNull.Value) //返回true

if (row["myCol"] == null) //返回false

if (row.IsNull("myCol")) //返回true

关键是如果你只是检查null或DBNull.Value使用IsNull,如果你只是检查DBNull.Value明确说出来并使用它。

答案 2 :(得分:3)

一个人打字少了。除此之外,我认为它们是等价的。

试着澄清为什么我说它们是等同的。

[Test()]
public void test() {
    var t = new System.Data.DataTable();
    t.Columns.Add("col1");
    var r = t.NewRow();

        // null is converted to DBNull.Value by DataRow
        r["col1"] = null;
        Assert.IsFalse(r["col1"] == null);
        Assert.IsTrue(r["col1"] == DBNull.Value);
        Assert.IsTrue(r.IsNull("col1"));

        // nullable types w/o values are also converted
        int? val = null;
        Assert.IsFalse(val.HasValue);
        r["col1"] = val;
        Assert.IsTrue(r["col1"] == DBNull.Value);
        Assert.IsTrue(r.IsNull("col1"));


}

答案 3 :(得分:0)

FWIW,我写了一堆DataRow扩展方法 - CastAsXXX() - 以避免必须处理DB可空性......或者至少将它推迟一点B ^)。这是我的CastAsInt()CastAsIntNullable()方法:

#region downcast to int

public static int CastAsInt( this DataRow row , int index )
{
  return toInt( row[index] ) ;
}
public static int CastAsInt( this DataRow row , string columnName )
{
  return toInt( row[columnName] ) ;
}

public static int? CastAsIntNullable( this DataRow row , int index )
{
  return toIntNullable( row[index] );
}
public static int? CastAsIntNullable( this DataRow row , string columnName )
{
  return toIntNullable( row[columnName] ) ;
}

#region conversion helpers

private static int toInt( object o )
{
  int value = (int)o;
  return value;
}

private static int? toIntNullable( object o )
{
  bool hasValue = !( o is DBNull );
  int? value    = ( hasValue ? (int?) o : (int?) null ) ;
  return value;
}

#endregion conversion helpers

#endregion downcast to int

用法非常简单。你只需要预先陈述你的期望。

DataRow dr = GetADataRowFromSomewhere() ;
// Throws NullReferenceException if the column is null
int     x  = dr.CastAsInt(         "column_1" ) ;
// Is perfectly happy with nulls (as it should be)
int?    y  = dr.CastAsIntNullable( "column_1" ) ;

我试图让它们变得通用,但没有骰子,除非我愿意将数据库中的NULL与类型的默认值(例如,数字类型为0)相关联,我不是。

答案 4 :(得分:0)

它使表中的行具有检查空值

if (! DBNull.Value.Equals(dataset.Tables["tablename"].Rows[n][0].ToString())) {
    //enter code here
} else {
  //enter code here
}
相关问题