在ASP.NET中如何将事务放在两个方法中处理?
请教高手:一个页面中有两个按钮,一个按钮用来读取数据并绑定到控件,开始事务,数据修改后,点击第二按钮后提交事务,保存修改。这两个按钮事件如何写?以下是我写的方法,但不能实现,该如何修改?
SqlConnection con = DB.dbcon();
SqlTransaction tran=null;
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
yuejuantimu();
}
}
public void yuejuantimu()
{
string cmdsql = "select top 5 答案ID,t.科目,内容,考生答案,空后内容,参考答案,分值 from 答案评卷 d ";
cmdsql += " WITH (updlock,readpast) inner join 试题库 t on t.试题ID=d.试题ID where t.科目= '";
cmdsql += Request.QueryString["rjxk"].ToString()+ "' and t.题型='";
cmdsql += DropDownList1.SelectedValue +"' and 小题得分 is null ORDER BY 答案ID ";
SqlCommand cmd = new SqlCommand(cmdsql, con);
SqlDataAdapter dpt = new SqlDataAdapter(cmd);
try
{
con.Open();
tran = con.BeginTransaction(IsolationLevel.RepeatableRead);
cmd.Transaction = tran;
DataTable dt = new DataTable();
dpt.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
Button1.Enabled = false;
}
catch
{
tran.Rollback();
con.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string upstr = "";
foreach (DataListItem list in DataList1.Items)
{
Label lbl = (Label)list.FindControl("lblID");
TextBox txtda = (TextBox)list.FindControl("txtDaan");
upstr += " update 答案评卷 set 小题得分=" + txtda.Text + " where 答案ID=" + lbl.Text ;
}
con.Open();
SqlCommand cmd = new SqlCommand(upstr, con);
tran = con.BeginTransaction(IsolationLevel.RepeatableRead);
cmd.Transaction = tran;
cmd.ExecuteNonQuery(); //执行到这里出现 :超时时间已到。在操作完成之前超时时间已过或服务器未响应。
();
con.Close();
}
请不吝敕教!