同时对两张表执行update和insert
cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["cn"]);
cn.Open();
SqlCommand cmd = new SqlCommand("UPDATE book SET T1= '" + Label1.Text.Replace("'", "''") + "'+'" + "/" + "'+'" + DropDownList3.Text + "'+'" + "/" + "'+'" + DropDownList1.Text + "'+'" + "-" + "'+'" + DropDownList2.Text + "'+'" + "/" + "'+ '" + TextBox4.Text.Replace("'", "''") + "'+'" + "/" + "'+'" + TextBox3.Text.Replace("'", "''") + "'+'" + "/" + "'+'" + Labelp.Text.Replace("'", "''") + "'+'" + "/" + "'+'" + Labelv.Text.Replace("'", "''") + "'+'" + "/" + "'+'" + TextBox5.Text.Replace("'", "''") + "' where booktime= '" + TextBox1.Text.Replace("'", "''") + "'", cn);
cmd.ExecuteNonQuery();
cn.Close();
Response.Write("<script>alert('预定成功')</script>");
string strm1 = "server=po-pc;database=aspnet;uid='sa';pwd='123'";
SqlConnection con = new SqlConnection(strm1);//创建连接对象
con.Open();
SqlCommand cmdm1 = new SqlCommand("insert into details (booktime,bookname) value ('" + TextBox1.Text + "', '" + Label1.Text.Replace("'", "''") + "')", con);
cmdm1.ExecuteNonQuery();//执行SQL命令
con.Close();
对booktime表执行update,对detail表执行insert,但是update执行了,insert没有执行
[解决办法]
你按照我这个改吧,我刚刚特地测试过了,通过!
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=.;database=test;pwd=123456;uid=sa;";//xxx连接字符串
con.Open();
SqlTransaction st = con.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.Transaction = st;
try
{
string sql1 = "insert into test(id) values('1')";
string sql2 = "update test set id='22' where id='1'";
string[] sqls = new string[] { sql1, sql2 };
foreach (string item in sqls)
{
cmd.CommandText = item;
cmd.ExecuteNonQuery();
}
st.Commit();
}
catch
{
st.Rollback();
}