SqlConnection conn = new SqlConnection("server=localhost;database=shoupiao;uid=sa;pwd=");
string sql1 = "delete from zwxx where checi='" + DropDownList1 .SelectedValue+ "'";
string sql2 = "delete from wpxx where checi='" + DropDownList1.SelectedValue + "'";
string sql3 = "delete from chpksh where checi='" + DropDownList1.SelectedValue + "'";
string sql4 = "delete from chyush where checi='" + DropDownList1.SelectedValue + "'";
string sql5 = "delete from zhannode where checi='" + DropDownList1.SelectedValue + "'";
string sql6 = "delete from checilj where checi='" + DropDownList1.SelectedValue + "'";
string sql7 = "delete from checixx where checi='" + DropDownList1.SelectedValue + "'";
SqlCommand cmd1 = new SqlCommand(sql1,conn);
SqlCommand cmd2 = new SqlCommand(sql2, conn);
SqlCommand cmd3 = new SqlCommand(sql3, conn);
SqlCommand cmd4 = new SqlCommand(sql4, conn);
SqlCommand cmd5 = new SqlCommand(sql5, conn);
SqlCommand cmd6 = new SqlCommand(sql6, conn);
SqlCommand cmd7 = new SqlCommand(sql7, conn);
conn.Open();
cmd1.Transaction = trans;
conn.BeginTransaction();
try
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
cmd5.ExecuteNonQuery();
cmd6.ExecuteNonQuery();
cmd7.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
Response.Write("<script language='javascript'>alert('删除失败!');</script>");
}
conn.Close(); }
string sql1 = "delete from zwxx where checi='" + DropDownList1 .SelectedValue+ "'";
string sql2 = "delete from wpxx where checi='" + DropDownList1.SelectedValue + "'";
string sql3 = "delete from chpksh where checi='" + DropDownList1.SelectedValue + "'";
string sql4 = "delete from chyush where checi='" + DropDownList1.SelectedValue + "'";
string sql5 = "delete from zhannode where checi='" + DropDownList1.SelectedValue + "'";
string sql6 = "delete from checilj where checi='" + DropDownList1.SelectedValue + "'";
string sql7 = "delete from checixx where checi='" + DropDownList1.SelectedValue + "'";
SqlCommand cmd1 = new SqlCommand(sql1,conn);
SqlCommand cmd2 = new SqlCommand(sql2, conn);
SqlCommand cmd3 = new SqlCommand(sql3, conn);
SqlCommand cmd4 = new SqlCommand(sql4, conn);
SqlCommand cmd5 = new SqlCommand(sql5, conn);
SqlCommand cmd6 = new SqlCommand(sql6, conn);
SqlCommand cmd7 = new SqlCommand(sql7, conn);
conn.Open();
cmd1.Transaction = trans;
conn.BeginTransaction();
try
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
cmd5.ExecuteNonQuery();
cmd6.ExecuteNonQuery();
cmd7.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
Response.Write("<script language='javascript'>alert('删除失败!');</script>");
}
conn.Close(); }
最好是写成一个cmd执行.
或者是写成一个存储过程,传DropDownList1.SelectedValue这个参数即可.
没有必要执行7条这样的,
貌似调用了多个事务并行执行.
cmd2.Transaction=trans;
cmd3.Transaction=trans;
cmd4.Transaction=trans;
cmd5.Transaction=trans;
cmd6.Transaction=trans;
cmd7.Transaction=trans;
string sql1 = "delete from zwxx where checi='" + DropDownList1 .SelectedValue+ "'";
string sql2 = "delete from wpxx where checi='" + DropDownList1.SelectedValue + "'";
string sql3 = "delete from chpksh where checi='" + DropDownList1.SelectedValue + "'";
string sql4 = "delete from chyush where checi='" + DropDownList1.SelectedValue + "'";
string sql5 = "delete from zhannode where checi='" + DropDownList1.SelectedValue + "'";
string sql6 = "delete from checilj where checi='" + DropDownList1.SelectedValue + "'";
string sql7 = "delete from checixx where checi='" + DropDownList1.SelectedValue + "'";
SqlCommand cmd1 = new SqlCommand(sql1,conn);
SqlCommand cmd2 = new SqlCommand(sql2, conn);
SqlCommand cmd3 = new SqlCommand(sql3, conn);
SqlCommand cmd4 = new SqlCommand(sql4, conn);
SqlCommand cmd5 = new SqlCommand(sql5, conn);
SqlCommand cmd6 = new SqlCommand(sql6, conn);
SqlCommand cmd7 = new SqlCommand(sql7, conn);
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
try
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
cmd5.ExecuteNonQuery();
cmd6.ExecuteNonQuery();
cmd7.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
Response.Write("<script language='javascript'>alert('删除失败!');</script>");
}
conn.Close(); }现在不报错了,但是返回删除失败!
{
cmd1.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
cmd5.ExecuteNonQuery();
cmd6.ExecuteNonQuery();
cmd7.ExecuteNonQuery();
trans.Commit();
}
catch(Exception ex)
{
trans.Rollback();
Response.Write(ex.Message);
}