日期:2014-05-17 浏览次数:20464 次
try { using (TransactionScope scope = new TransactionScope()) { //更新northwind数据库的Employees表 using (SqlConnection conOne = new SqlConnection("server=.;uid=sa;pwd=123;database=northwind")) { conOne.Open(); SqlCommand command = new SqlCommand("update Employees set lastname='chen' where employeeid='1'", conOne); int i = command.ExecuteNonQuery(); } //更新pubs数据库的jobs表 using (SqlConnection conTwo = new SqlConnection("server=.;uid=sa;pwd=123;database=pubs")) { conTwo.Open(); SqlCommand command = new SqlCommand("update jobs set job_desc='chen' where job_id='1'", conTwo); int i = command.ExecuteNonQuery(); } scope.Complete(); //提交事物 } } catch (Exception ex) //发生异常后自动回滚 { //throw; }