string connstr = "Data Source=ZW;Initial Catalog=code;Integrated Security=True";
using(TransactionScope ts = new TransactionScope())
{
try
{
string sql = "insert into bookInfo(name,address,author) values('aa','bb','cc') ";
string sql2 = "insert into bookInfo5(name,address,author) values('zzzz','zzzz','zz')";//不存在的表
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlCommand cmd2 = new SqlCommand(sql2, conn);
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
ts.Complete();
}
catch (Exception ee)
{
Console.Write("xxx"+ee.ToString());
}
}
文件的创建或添加是不会回滚的