无法使用以下两种方式检查空值C#datareader空值错误

时间:2014-03-20 06:42:36

标签: c# sql null datareader

问题是;我在类中使用此代码并从我的主窗体调用类。我收到有关我的datareader无法获取null值的错误。如何检查来自sp的后续数据不为空。作为内部代码,如果我看到null我改变它并返回为outval(0000)

public string opt()
{
   string strng= "";
   string outval= "";
   SqlConnection conn = new SqlConnection();
   conn.ConnectionString = ConfigurationManager.ConnectionStrings["nmrbg"].ConnectionString;
   conn.Open();
   SqlCommand cmd = new SqlCommand("nmp_sp_sy", conn);
   cmd.CommandType = CommandType.StoredProcedure;
   SqlDataReader rdr = cmd.ExecuteReader();    
   // if (rdr.HasRows)
   // {
   //     while (rdr.Read())
   //     {
   //        int nm= rdr.GetInt32(0);
   //        strng= Convert.ToString(nm);
   //        outval= strng;
   //     }
   // }
   while (rdr.Read())
   {
      int nm= rdr.GetInt32(0);
      strng= Convert.ToString(nm);
      outval= strng;
   }
   if (!rdr.HasRows)
   {
      outval= "0000";
   }
   rdr.Close();
   rdr.Dispose();
   conn.Close();
   conn.Dispose();
   return outval;
}

`

Sp是;

Begin

    Begin Transaction
    DECLARE @minSira int

    select  @minSira=Min(siraNo) 
    from nmr_tbl_srbklynlr 
    where turId=2 and aktifPasif=1

    UPDATE top (1) nmr_tbl_srbklynlr
    SET aktifPasif = 0
    Where siraNo=@minSira

    select @minSira
    Commit Transaction

End

2 个答案:

答案 0 :(得分:1)

试试这段代码

public string opt()
{
    string strng= "";
    string outval= "";
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString =      ConfigurationManager.ConnectionStrings["nmrbg"].ConnectionString;
    conn.Open();
    SqlCommand cmd = new SqlCommand("nmp_sp_sy", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataReader rdr = cmd.ExecuteReader();    
    // if (rdr.HasRows)
    // {
   //     while (rdr.Read())
   //     {
   //        int nm= rdr.GetInt32(0);
   //        strng= Convert.ToString(nm);
   //        outval= strng;
   //     }
    // }
       while (rdr.Read())
       {
           if(rdr["ColumnName"] != DBNull.Value)
           {
               int nm= rdr.GetInt32(0);
               strng= Convert.ToString(nm);
               outval= strng;
           }
            else
           {
                  outval= "0000";
           }
       }
    if (!rdr.HasRows)
    {
        outval= "0000";
    }
       rdr.Close();
       rdr.Dispose();
       conn.Close();
       conn.Dispose();
       return outval;
  }

答案 1 :(得分:0)

测试 null 上的返回值:数据库很可能在 int 上返回 null 。 将IDisposable换入使用:可以防止资源泄露正常流量和异常。

public string opt() {
  using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["nmrbg"].ConnectionString)) {
    conn.Open();

    using (SqlCommand cmd = new SqlCommand("nmp_sp_sy", conn)) {
      cmd.CommandType = CommandType.StoredProcedure;

      using (SqlDataReader rdr = cmd.ExecuteReader()) {
        // If there's no records to read (rdr is empty) return default
        if (!rdr.Read())
          return "0000";

        Object rawData = rdr.GetValue(0);

        if (Object.RefrenceEquals(null, rawData))  
          return "0000"; // <- Or whatever on null value
        else
          return rawData.ToString();    
      }
    }
  }
}