参考代码:
引用Dapper
public bool OrderAdd2(User user,Order order) { string dbString = ConfigurationManager.ConnectionStrings["TDB"].ConnectionString; string db2String = ConfigurationManager.ConnectionStrings["TDB2"].ConnectionString; sqlConnection conn = new sqlConnection(dbString); sqlConnection conn2 = new sqlConnection(db2String); conn.open(); conn2.open(); sqlTransaction tran = conn.BeginTransaction(); sqlTransaction tran2 = conn2.BeginTransaction(); //sqlCommand cmd = conn.CreateCommand(); //cmd.CommandText = $"insert into [User](USex,Uname)values(‘{user.USex}‘,‘{user.Uname}‘)"; //cmd.Transaction = tran; //sqlCommand cmd2 = conn2.CreateCommand(); //cmd2.CommandText = $"insert into [Order](UID,Date)values(‘{order.UID}‘,‘{order.Date}‘)"; //cmd2.Transaction = tran2; string cmdTxt = $"insert into [User](USex,Uname)values(@USex,@Uname)"; string cmdTxt2 = $"insert into [Order](UID,Date)values(@UID,@Date)"; try { //cmd.ExecuteNonquery(); //cmd2.ExecuteNonquery(); conn.Execute(cmdTxt,user,tran); conn2.Execute(cmdTxt2,order,tran2); tran.Commit(); tran2.Commit(); return true; } catch (Exception ex) { tran.Rollback(); tran2.Rollback(); return false; } finally { conn.Close(); conn2.Close(); } }总结
以上是内存溢出为你收集整理的SQLServer同一实例下事务 *** 作全部内容,希望文章能够帮你解决SQLServer同一实例下事务 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)