public Boolean ExecSqlSelect(string sQueryString)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
        con.Open();
        SqlCommand dbCommand = new SqlCommand(sQueryString, con);
                if (dbCommand.ExecuteScalar().ToString!= null)
        {
            con.Close();
            return true;
        }
        else
        {
            con.Close();
            return false;
        }
    }
老是说为将对象的引用设置到对象的实例!!

解决方案 »

  1.   

    if (dbCommand.ExecuteScalar().ToString!= null)
    这句话不出错,真奇怪??Tostring()..
      

  2.   

    dbCommand.ExecuteScalar(),如果没有返回值就会出错的(查询结果第一行第一列的值为NULL)
    如果单纯判断NULL,去年tostring()
      

  3.   

    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"].toString());
      
      

  4.   

    public Boolean ExecSqlSelect(string sQueryString)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
            con.Open();
            SqlCommand dbCommand = new SqlCommand(sQueryString, con);
             return  dbCommand.ExecuteScalar() != null
           }
      

  5.   

    if ([color=#FF0000]dbCommand.ExecuteScalar().ToString好像后面要加个()的把。
      

  6.   

    if (dbCommand.ExecuteScalar().ToString!= null)
    等于null 不需要转换的
      

  7.   

    对,3楼说的很对,这个问题我也出现过,所以记忆深刻。
    你只要稍微短点调试一下就清楚了。
    原因就是你的dbCommand.ExecuteScalar()直接判断是否空就可以了
    但是你的问题是:如果dbCommand.ExecuteScalar()为空,再.ToString()的话
    问题就出来啦。
      

  8.   

    if (dbCommand.ExecuteScalar().ToString!= null)既然是tostring() 还等于null 合理吗?要么就=“”
      

  9.   

    xiedaxia
    谢谢大侠!!
      

  10.   

       把这句中的.ToString()去掉
    if (dbCommand.ExecuteScalar().ToString!= null)