大家来看一下这个跨库事务,有什么问题?
2013-11-01 16:45
411 查看
protected void Button1_Click(object sender, EventArgs e) { string msg = ""; Dictionary<string, List<ExecuteCMD>> oExecuteCMDMapList = new Dictionary<string, List<ExecuteCMD>>(); List<ExecuteCMD> oExecuteCMDList1 = new List<ExecuteCMD>(); ExecuteCMD oExecuteCMD1 = new ExecuteCMD(); oExecuteCMD1.CommandText = "INSERT INTO TEST1(VALUE1,VALUE2,VALUE3,VALUE4,VALUE5) VALUES('1','1','1','1','1')"; oExecuteCMD1.oParameter = null; oExecuteCMDList1.Add(oExecuteCMD1); ExecuteCMD oExecuteCMD2 = new ExecuteCMD(); oExecuteCMD2.CommandText = "INSERT INTO TEST1(VALUE1,VALUE2,VALUE3,VALUE4,VALUE5) VALUES('1','1','1','1','1')"; oExecuteCMD2.oParameter = null; oExecuteCMDList1.Add(oExecuteCMD2); oExecuteCMDMapList.Add("server=192.168.25.195;uid=sa;password=sasasa;database=DB1", oExecuteCMDList1); List<ExecuteCMD> oExecuteCMDList2 = new List<ExecuteCMD>(); ExecuteCMD oExecuteCMD11 = new ExecuteCMD(); oExecuteCMD11.CommandText = "INSERT INTO TEST1(VALUE1,VALUE2,VALUE3,VALUE4,VALUE5) VALUES('2','2','2','2','2')"; oExecuteCMD11.oParameter = null; oExecuteCMDList2.Add(oExecuteCMD11); ExecuteCMD oExecuteCMD22 = new ExecuteCMD(); oExecuteCMD22.CommandText = "INSERT INTO TEST1(VALUE1,VALUE2,VALUE3,VALUE4,VALUE5) VALUES('2','2','2','2','2')"; oExecuteCMD22.oParameter = null; oExecuteCMDList2.Add(oExecuteCMD22); oExecuteCMDMapList.Add("server=192.168.25.195;uid=sa;password=sasasa;database=DB2", oExecuteCMDList2); bool bTrue = false; bTrue = DoExecute(oExecuteCMDMapList, out msg); switch (bTrue) { case true: this.Response.Write("操作成功!"); break; case false: this.Response.Write("操作失败:" + msg); break; } } /// <summary> /// 执行跨库事务操作 /// </summary> /// <param name="oExecuteCMDMapList">多库的连接字符串,每个库的执行命令</param> /// <param name="msg">返回的信息</param> /// <returns>TRUE:成功/FALSE:失败</returns> public bool DoExecute(Dictionary<string, List<ExecuteCMD>> oExecuteCMDMapList, out string msg) { bool bTrue = false; try { foreach (var oExecuteCMDList in oExecuteCMDMapList) { if (oExecuteCMDList.Key == "") { msg = "数据连接串为空!"; return bTrue; } foreach (var oExecuteCMD in oExecuteCMDList.Value) { if (oExecuteCMD.CommandText == "") { msg = "执行的命令不能为空!"; return bTrue; } } } } catch (Exception Ex) { msg = Ex.Message; return bTrue; } List<SqlConnection> oSqlConnectionList = new List<SqlConnection>(); List<SqlCommand> oSqlCommandList = new List<SqlCommand>(); try { foreach (var oExecuteCMDList in oExecuteCMDMapList) { SqlConnection oSqlConnection = new SqlConnection(oExecuteCMDList.Key); oSqlConnectionList.Add(oSqlConnection); SqlCommand oSqlCommand = new SqlCommand(); oSqlCommandList.Add(oSqlCommand); oSqlConnection.Open(); SqlTransaction oSqlTransaction = oSqlConnection.BeginTransaction(); oSqlCommand.Connection = oSqlConnection; oSqlCommand.Transaction = oSqlTransaction; foreach (var oExecuteCMD in oExecuteCMDList.Value) { oSqlCommand.CommandText = oExecuteCMD.CommandText; if (oExecuteCMD.oParameter != null) { foreach (var o in oExecuteCMD.oParameter) { oSqlCommand.Parameters.Add(o); } } oSqlCommand.ExecuteNonQuery(); oSqlCommand.Parameters.Clear(); } } foreach (SqlCommand oTempSqlCommand in oSqlCommandList) { oTempSqlCommand.Transaction.Commit(); } bTrue = true; msg = "成功"; } catch (Exception Ex) { foreach (SqlCommand oTempSqlCommand in oSqlCommandList) { oTempSqlCommand.Transaction.Rollback(); } msg = Ex.Message; } finally { for (int i = 0; i < oSqlConnectionList.Count; i++) { oSqlCommandList[i].Dispose(); oSqlConnectionList[i].Close(); oSqlConnectionList[i].Dispose(); } } return bTrue; } /// <summary> /// 操作命令 /// </summary> public class ExecuteCMD { /// <summary> /// 命令 /// </summary> public string CommandText { set; get; } /// <summary> /// 参数 /// </summary> public Parameter[] oParameter { set; get; } }
相关文章推荐
- 请大家帮忙解决一下这个问题,十分感谢!
- 大家帮我看看这个php语句有什么问题
- DLL中的Forms问题,大家来看一下啊!!
- 大家有没有考虑过这个问题:datagrid分页,viewstate到底起什么作用?
- 关于缓冲区的问题, 大家讨论一下.
- 端口占用问题,今天学习的时候又出现了这个问题,找了度娘,特此记录以下。第一次写博客,希望自己能坚持下来。大家共同进步
- 谁能帮我解决一下这个问题
- <T extends Serializable>这是什么意思呢?看明白这个,你的问题就自然而然的明白了!
- 音视频 播放每次出现这个log信息 播放就会明显得卡顿一下 ,各位帮忙看下什么原因,该怎么处理啊 新手求指教
- 关于JAVA中包装类的是什么类型传递这个问题的笔记
- 一个简单的跨库事务问题
- 大家知道什么是 asp.net 呢 ?学习一下
- ubuntu12.04安装opencv-1.0.0的问题(希望大家进来讨论一下)
- 整理了一下Asp.net源码常见问题(完善中...),欢迎大家补充修正(最后更新于06-01)!
- 我是新手,大家帮忙看看这个问题怎样解决,小弟先谢啦
- 大家看看这个随机发牌的小程序,给出点建议,或者挑出点瑕疵,一起来讨论一下
- Android面试题 你经常写复杂的界面,那Android刷新Ui有什么特点?在这个过程中做哪些操作会出现问题
- 不知道有什么好歌听大家推荐一下,刚才听了首白狐觉得不错拿出来共享
- 新的模版有一些问题,不知道大家有这个问题没有.
- 大家来分析一下,下面代码的严重问题