您的位置:首页 > 其它

大家来看一下这个跨库事务,有什么问题?

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; }
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐