我用的NextResult()怎么总是只能得到一行数据呀?SqlCommand Selectcmd = new SqlCommand("select PackageDetail_Info.Count,Project_Info.Price from PackageDetail_Info,Project_Info where PackageDetail_Info.Project_ID=Project_Info.Project_ID and Package_ID='" + Package_ID + "'", conn);
                    System.Data.SqlClient.SqlDataReader dr = Selectcmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Response.Write("<script language=javascript>alert('int count = dr.GetInt32(0);')</script>");
                        //for (int k = 0; k <= dr.FieldCount;k++ )
                        {
                            //Label2.Text = dr.GetInt32(0).ToString().Trim();
                            //Label3.Text = dr.FieldCount.ToString().Trim();
                            int count = dr.GetInt32(0);
                            Response.Write("<script language=javascript>alert('1')</script>");
                            double Price = dr.GetDouble(1);
                            total = total + count * Price;
                        }
                        if (dr.NextResult())
                        {
                            //dr.NextResult();
                            dr.Close();
                        }
                        else
                        {
                            dr.Close();
                            break;
                        }
                            total = total * double.Parse(Discount);
                    }

解决方案 »

  1.   

    去掉where看看。是不是只返回了1条数据。
      

  2.   

    这是去掉where在数据库中得到的数据:
    count   price
    2 2500
    1 1500
    3 2700
    1 2500
    1 1500
    3 3000
    3 2700
    2 2500
    1 1500
    2 3000
    3 2700
    2 1000
    1 2500
    1 3000
    1 2700
    1 2500
    2 1500
    2 2700
    4 999
      

  3.   

    Discount=0.85
    total算出是5000
      

  4.   

    此处不应该关闭吧。
    if (dr.NextResult())
                            {
                                //dr.NextResult();
                                dr.Close();
                            }
      

  5.   

     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            //string Customer_ID = Session["Customer_IDMax"].ToString().Trim();
            string Customer_ID = "000000001";
            int j = 0;
            double total = 0;
            System.Data.SqlClient.SqlConnection conn = new
    System.Data.SqlClient.SqlConnection("server=Robert-pc\\Sqlexpress;uid=sa;pwd=sa;database=DB_wgqzpc");
            try
            {
                conn.Open();
                for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                {
                    CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("Checking");
                    if (cbox.Checked == true)
                    {
                        TextBox cout = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
                        string Package_ID = GridView1.Rows[i].Cells[1].Text.ToString().Trim();
                        string Package = GridView1.Rows[i].Cells[2].Text.ToString().Trim();
                        string Discount = GridView1.Rows[i].Cells[3].Text.ToString().Trim();
                        //string User_ID = Session["User_ID"].ToString().Trim();
                        string User_ID = "000000001";
                        SqlCommand Selectcmd = new SqlCommand("select PackageDetail_Info.Count,Project_Info.Price from PackageDetail_Info,Project_Info where PackageDetail_Info.Project_ID=Project_Info.Project_ID and Package_ID='" + Package_ID + "'", conn);
                        System.Data.SqlClient.SqlDataReader dr = Selectcmd.ExecuteReader();
                        while (dr.Read())
                        {
                            Response.Write("<script language=javascript>alert('int count = dr.GetInt32(0);')</script>");
                            //for (int k = 0; k <= dr.FieldCount;k++ )
                            {
                                //Label2.Text = dr.GetInt32(0).ToString().Trim();
                                //Label3.Text = dr.FieldCount.ToString().Trim();
                                int count = dr.GetInt32(0);
                                Response.Write("<script language=javascript>alert('1')</script>");
                                double Price = dr.GetDouble(1);
                                total = total + count * Price;
                            }
                            if (dr.NextResult())
                            {
                                //dr.NextResult();
                                //dr.Close();
                            }
                            else
                            {
                                dr.Close();
                                break;
                            }
                                total = total * double.Parse(Discount);
                        }                    Session["Total"] = total;                    SqlCommand Insertcmd = new SqlCommand("INSERT INTO Package_Info VALUES ('" + Package_ID + "','" + Package + "','" + Customer_ID + "','" + total + "','" + User_ID + "','" + txtDate.Text + "','" + Discount + "')", conn);
                        j = j + Insertcmd.ExecuteNonQuery();//执行插入操作
                    }
                }
                if (j > 0)//如果插入成功  
                {
                    //Response.Write("<script language=javascript>alert('共删除" + j + "条记录')</script>");
                    //chkSelectAll.Checked = false;
                    GridView1.DataBind();            }
                else//如果插入失败 
                {
                    Response.Write("<script language=javascript>alert('新增失败!')</script>");
                }
            }
            catch (System.Exception ee)//进行异常处理 
            {            Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
            }
            finally
            {
                conn.Close();
            }
        }
    }
    这个是我的Click的完整事件
      

  6.   

    把这段代码注释掉吧
    if (dr.NextResult())
                            {
                                //dr.NextResult();
                                dr.Close();
                            }
                            else
                            {
                                dr.Close();
                                break;
                            }
      

  7.   

    24行处加断点 然后在看下Selectcmd的值  放到sql里去运行 看得出的到底是几条结果再做进一步判断 是否是代码的逻辑有问题
      

  8.   


    同意。不管怎么样,你都执行 dr.Close()。还想读什么“更多数据行”?另外,使用从DbDataReader继承来的Read()方法就行了,不要使用NextResult方法。
      

  9.   

    感谢njw1028,sp1234两位,问题已经解决了!
    小弟初学ASP.NET,以后还请大家多指点!
      

  10.   

    [code=csharp] if (lvstudent.Items.Count >0)
                {
                    lvstudent.Items.Clear();
                
                
                }
          code]