command.ExecuteScalar()取不到记录时,如何处理呢?
以下代码,我想获取书的库存数,但是,如果记录不存在,则取不到 册数,就会报错,
我应该如何写代码呢?
        //获取库存
 public int GetKuCun(string strywdh,string strsh,string strbc)
        {
            int iceshu;
            iceshu = 0;
            strsql = "select 册数 from cp_库存 where 业务单号='" + strywdh + "' and 书号='" + strsh + "' and 版次='" + strbc + "'";            command = new SqlCommand(strsql, mydb.conn);
            iceshu = (int)command.ExecuteScalar();
            return iceshu;
        }

解决方案 »

  1.   

    command.ExecuteScalar();是针对一条记录,一个列的情况,但是,如果
    记录查询不到,如何进行容错处理呢?
    在这里,如果查不到记录,就应该返回 0 
      

  2.   


    public int GetKuCun(string strywdh,string strsh,string strbc)
            {
    try
    {
                int iceshu;
                iceshu = 0;
                strsql = "select 册数 from cp_库存 where 业务单号='" + strywdh + "' and 书号='" + strsh + "' and 版次='" + strbc + "'";            command = new SqlCommand(strsql, mydb.conn);
                iceshu = (int)command.ExecuteScalar();
    }
    catch{
    return 0;
    }
                return iceshu;
            }
      

  3.   


      //获取库存
     public int GetKuCun(string strywdh,string strsh,string strbc)
            {
                int iceshu;
                iceshu = 0;
                strsql = "select 册数 from cp_库存 where 业务单号='" + strywdh + "' and 书号='" + strsh + "' and 版次='" + strbc + "'";            command = new SqlCommand(strsql, mydb.conn);
                object temp = command.ExecuteScalar();
                if(temp!=null)
                {
                    iceshu=(int)temp;
                }
                return iceshu;
            }
      

  4.   

    var val = command.ExecuteScalar();
    return val == null ? 0 : (int)val;
      

  5.   

    iceshu = (int)command.ExecuteScalar();
    强制转换,没记录是肯定出错
      

  6.   


     protected static int ExecuSqlValue(string strSQL)
            {            OleDbConnection myCn = new OleDbConnection(strconn);
                OleDbCommand myCmd = new OleDbCommand(strSQL, myCn);
                try
                {
                    myCn.Open();
                    object r = myCmd.ExecuteScalar();
                    if (Object.Equals(r, null))
                    {
                        throw new Exception("value unavailable!");                }
                    else
                    {
                        return (int)r;
                    }
                }
                catch (OleDbException e)
                {
                    throw new Exception(e.Message);
                }
                finally
                {
                    myCmd.Dispose();
                    myCn.Close();            }        }
      

  7.   

    先试试你的SQL语句监视下SQL 看看到底是那的问题
      

  8.   


            public int GetKuCun(string strywdh,string strsh,string strbc)
            {
                int iceshu;
                iceshu = 0;
                strsql = "select 册数 from cp_库存 where 业务单号='" + strywdh + "' and 书号='" + strsh + "' and 版次='" + strbc + "'";            command = new SqlCommand(strsql, mydb.conn);
                object temp = command.ExecuteScalar();
                return iceshu==null?0:(int)iceshu;
            }
      

  9.   


    正解 因为ExecuteScalar 返回的是一个object类型,所以如果你强制转换如果返回时null的话肯定报错。