我用的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);
}
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);
}
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
total算出是5000
if (dr.NextResult())
{
//dr.NextResult();
dr.Close();
}
{
//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的完整事件
if (dr.NextResult())
{
//dr.NextResult();
dr.Close();
}
else
{
dr.Close();
break;
}
同意。不管怎么样,你都执行 dr.Close()。还想读什么“更多数据行”?另外,使用从DbDataReader继承来的Read()方法就行了,不要使用NextResult方法。
小弟初学ASP.NET,以后还请大家多指点!
{
lvstudent.Items.Clear();
}
code]