DBNull if statement

前端 未结 9 1548
再見小時候
再見小時候 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 { .. }.

提交回复
热议问题