thisReader肯定不等于null,即使搜出来是0条数据
你可以用thisReader.Fill(ds);
然后判断ds.Tables[0].Rows.Count;

解决方案 »

  1.   

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
                SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection);            myConnection.Open();
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                int count = 0;            if (!thisReader.HasRows)//在搜索不到数据时,thisReader并不是null,而是无数据。
                {
                    return 0;
                }
                if (thisReader.Read())
                {
                    count = thisReader.GetInt32(0);
                }
                thisReader.Close();
                myConnection.Close();
                return count;
      

  2.   

      if (thisReader.Read())
      {
         count = thisReader.GetInt32(0);
      }
      else
      {
         return 0;
      }
      

  3.   

    看错。
    没结果if (thisReader.Read())还能进去?
      

  4.   

     SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
                SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection);            myConnection.Open();
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                int count = 0;            if (thisReader.GetSchemaTable().Rows.Count == 0) //修改这行就可以了。
                {
                    return 0;
                }
                if (thisReader.Read())
                {
                    count = thisReader.GetInt32(0);
                }
                thisReader.Close();
                myConnection.Close();
                return count;
      

  5.   

    if (thisReader== null)
    {
    return 0;
    }
    if (thisReader.Read())
    {
    count = thisReader.GetInt32(0);
    }
    ==》
    if (thisReader.Read())
    {
    count = thisReader.GetInt32(0);
    }
    else
    {
    return 0;
    }
      

  6.   

    最好不要直接 return 0;这样的数据连接就不会关闭
    else
    {
    count = 0;
    }
      

  7.   

    if(thisRead.HasRows)//记录集是否为空
    {
      while(thisRead.Read())
     
      {
        .....
      }
    }
      

  8.   

    有颜色的问题你看见下面那回帖的编辑器了没?A下面有个红色的那个A,选中你的要加颜色的文字点A就可以了。
      

  9.   

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
                SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection);            myConnection.Open();
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                int count = 0;            if (thisReader== null)
                {
                    return 0;
                }
                if (thisReader.Read())
                {
                    count = thisReader.GetInt32(0);
                }
                thisReader.Close();
                myConnection.Close();
                return count;
    sqldatareader是一行一行取数据
    怎么能fill(ds)那好像也没有用if(this.reader.read())这样用的啊
    这这去一条,第一条,没有得到这个的用途这个是这么用的
             ListItem newItem = new ListItem();
            ListBox1.Items.Clear();
            while (reader.Read())        {
                newItem = new ListItem();
                newItem.Text = reader["Creater"].ToString().Trim();
                newItem.Value = reader["HospitalID"].ToString().Trim();
                ListBox1.Items.Add(newItem);
            }
    用reader逐行给listbox绑定值
      

  10.   

    sqldatareader是一行一行取数据 
    怎么能fill(ds)那 好像也没有用if(this.reader.read())这样用的啊 
    这这去一条,第一条,没有得到这个的用途 这个是这么用的 
            ListItem newItem = new ListItem(); 
            ListBox1.Items.Clear(); 
            while (reader.Read())        { 
                newItem = new ListItem(); 
                newItem.Text = reader["Creater"].ToString().Trim(); 
                newItem.Value = reader["HospitalID"].ToString().Trim(); 
                ListBox1.Items.Add(newItem); 
            } 
    用reader逐行给listbox绑定值 
      

  11.   

    sqldatareader有一个isnull函数
    你看下
    或者是isnul
      

  12.   


            SqlDataReader dr = new SqlCommand("command string", new SqlConnection("connection string")).ExecuteReader();
            while (dr.Read())
            {        }
      

  13.   

    我都做过实验了,reader.hasrows都为ture啊 
      

  14.   

    你要注意 你的这个 select 语句 如果没有错误的话 是肯定会返回一条记录的..
    在数据没有合适数据的时候 也就是 结果是count 为0 
    hasrows是肯定会为true的
    不能使用这个判断
    if (thisReader.Read())//这里要记住 一但成功执行了Read()方法数据就开始读取到第一行了.如果要读取多条数据 使用while语句
    {
       count = thisReader.GetInt32(0);
    }
    else
       return 0;
      

  15.   

    if(thisRead.HasRows)
    {
      while(thisRead.Read())
     
      {
        .....
      }
    }
      

  16.   

     if(thisReader.isDBnull(0)==false)//判断是否读出的为Null 答案是这样的
      

  17.   

                if (thisReader.Read())
                {
                    count = thisReader.GetInt32(0); //这出错是因为你查到的数据,第一列值,非Int32类型吧.
                }
    如果想在这边处理的话,,
    就在这加个try () {} catch () {} 如下.
    if (thisReader.Read())
                {
    try ()
    {
                    count = thisReader.GetInt32(0); //这出错是因为你查到的数据,第一列值,非Int32类型吧.
    }
    catch (Exception e )
    {
    count = 0 ;
    MessageBox.Show (e.Message); // 这个只是会提示异常信息.你不喜欢的话,随你去掉好了!
    }            }
      

  18.   

     if (thisReader.Read()) 能进去就说明不为空啊. 
    你的thisReader.GetInt32(0);  是用来取thisReader当前行 第一列里面的int32数据. 
      

  19.   

    支持一下!SqlDataReader······
      

  20.   

            SqlDataReader dr = new SqlDataReader();
            if (dr.Read()) //或if (dr.HasRows)
            {
                //有内容
            }
            else
            {
                //无内容
            }
      

  21.   

    HasRows属性,或者thisReader.Read()
      

  22.   

                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                    Response.End();
                }
                finally
      

  23.   


    //应该是这样吧?
    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
                SqlCommand thisCommand = new SqlCommand("select sum(point) as count from test where stuId='" + stuId + "'and courseName='" + courseName + "'and type='" + type+ "'", myConnection);            myConnection.Open();
                SqlDataReader thisReader = thisCommand.ExecuteReader();
                int count = 0;            if (thisReader== null)
                {
                    return 0;
                    thisReader.Close();
                    myConnection.Close();
                }
                if (thisReader.Read())
                {
                    count = Int32.Parse(thisReader["count"].ToString());
                }
                thisReader.Close();
                myConnection.Close();
                return count;
      

  24.   

    using (SqlDataReader dr = cmdSql.ExecuteReader())
                {
                    if (dr!=null)
                    {
                        dt.Load(dr);
                    }
                    dr.Close();
                }
      

  25.   

    吧你的thisReader == null改成
         if (thisReader.GetSchemaTable().Rows.Count == 0)
         { 
                    return 0; 
        } 
      

  26.   

    用 Bool 型判断?……
    就两种情况:读到和没读到……
    是吧?简单吧……晕……
      

  27.   

    reader.read() = true,表示读到的有内容;相反没有读到内容
    if (thisReader== null)
                {
                    return 0;
                }
                if (thisReader.Read())
                {
                    count = thisReader.GetInt32(0);
                }可以修改为
    if thisReader.reader()
    { count = thisReader.GetInt32(0);
    }
    else 
    {
    return 0;
    }