DBNull if statement

前端 未结 9 1536
再見小時候
再見小時候 2020-12-01 13:55

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

相关标签:
9条回答
  • 2020-12-01 14:14

    This should work.

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

    also need to add using statement, like bellow:

    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 { .. }.

    0 讨论(0)
  • 2020-12-01 14:17

    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.

    0 讨论(0)
  • 2020-12-01 14:23

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

    Convert.IsDBNull()
    

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

    0 讨论(0)
提交回复
热议问题