C# DBNull if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10431835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:50:13  来源:igfitidea点击:

DBNull if statement

c#dbnull

提问by PipBoy

I'm trying to execute a stored procedure and then use an if statement to check for null values and I'm coming up short. I'm a VB guy so please bear with me if I'm making a schoolboy syntax error.

我正在尝试执行一个存储过程,然后使用 if 语句来检查空值,但我正在做空。我是一个 VB 人,所以如果我犯了一个小学生语法错误,请多多包涵。

objConn = new SqlConnection(strConnection);
objConn.Open();
objCmd = new SqlCommand(strSQL, objConn);
rsData = objCmd.ExecuteReader();
rsData.Read();

if (!(rsData["usr.ursrdaystime"].Equals(System.DBNull.Value)))
        {
            strLevel = rsData["usr.ursrdaystime"].ToString();

        }

Would this allow me to check whether the SQL connection is returning just a value and if so then populating my string?

这是否允许我检查 SQL 连接是否仅返回一个值,如果是,则填充我的字符串?

I'm used to being able to just check the below to see if a value is being returned and not sure I'm doing it correctly with C#

我习惯于只检查下面的内容以查看是否返回了值,但不确定我是否使用 C# 正确执行此操作

If Not IsDBNull(rsData("usr.ursrdaystime"))

如果不是 IsDBNull(rsData("usr.ursrdaystime"))

Any help would be appreciated!

任何帮助,将不胜感激!

采纳答案by Kamil Lach

This should work.

这应该有效。

if (rsData["usr.ursrdaystime"] != System.DBNull.Value))
{
    strLevel = rsData["usr.ursrdaystime"].ToString();
}

also need to add using statement, like bellow:

还需要添加 using 语句,如下所示:

using (var objConn = new SqlConnection(strConnection))
     {
        objConn.Open();
        using (var objCmd = new SqlCommand(strSQL, objConn))
        {
           using (var rsData = objCmd.ExecuteReader())
           {
              while (rsData.Read())
              {
                 if (rsData["usr.ursrdaystime"] != System.DBNull.Value)
                 {
                    strLevel = rsData["usr.ursrdaystime"].ToString();
                 }
              }
           }
        }
     }

this'll automaticly dispose (close) resources outside of block { .. }.

这将自动处理(关闭)块 { .. } 之外的资源。

回答by jason

The idiomatic way is to say:

惯用的方式是说:

if(rsData["usr.ursrdaystime"] != DBNull.Value) {
    strLevel = rsData["usr.ursrdaystime"].ToString();
}

This:

这个:

rsData = objCmd.ExecuteReader();
rsData.Read();

Makes it look like you're reading exactly one value. Use IDbCommand.ExecuteScalarinstead.

使您看起来像是在读取一个值。使用IDbCommand.ExecuteScalar来代替。

回答by mgnoonan

Yes, just a syntax problem. Try this instead:

是的,只是一个语法问题。试试这个:

if (reader["usr.ursrdaystime"] != DBNull.Value)

.Equals()is checking to see if two Object instances are the same.

.Equals()正在检查两个 Object 实例是否相同

回答by Marc Gravell

Consider:

考虑:

if(rsData.Read()) {
  int index = rsData.GetOrdinal("columnName"); // I expect, just "ursrdaystime"
  if(rsData.IsDBNull(index)) {
     // is a null
  } else {
     // access the value via any of the rsData.Get*(index) methods
  }
} else {
  // no row returned
}

Also: you need more using;p

另外:你需要更多using;p

回答by Likurg

At first use ExecuteScalar

首先使用 ExecuteScalar

 objConn = new SqlConnection(strConnection);
 objConn.Open();
 objCmd = new SqlCommand(strSQL, objConn);
 object result = cmd.ExecuteScalar();
 if(result == null)
     strLevel = "";
 else 
     strLevel = result.ToString();

回答by Cade Roux

The closest equivalent to your VB would be (see this):

与您的 VB 最接近的等价物是(请参阅此):

Convert.IsDBNull()

But there are a number of ways to do this, and most are linked from here

但是有很多方法可以做到这一点,大多数都从这里链接

回答by dstineback

I use String.IsNullorEmpty often. It will work her because when DBNull is set to .ToString it returns empty.

我经常使用 String.IsNullorEmpty。它会起作用,因为当 DBNull 设置为 .ToString 时,它返回空。

if(!(String.IsNullorEmpty(rsData["usr.ursrdaystime"].toString())){
        strLevel = rsData["usr.ursrdaystime"].toString();
    }

回答by GoranSu

Ternary operator should do nicely here: condition ? first_expression : second_expression;

三元运算符在这里应该做得很好: 条件?第一个表达式:第二个表达式;

strLevel = !Convert.IsDBNull(rsData["usr.ursrdaystime"]) ? Convert.ToString(rsData["usr.ursrdaystime"]) : null

strLevel = !Convert.IsDBNull(rsData["usr.ursrdaystime"]) ?Convert.ToString(rsData["usr.ursrdaystime"]) : null