#region 跨数据库 事务操作
public bool ExecTransactionScope(System.Collections.Generic.List<TransactionScopeModel> list)
{
bool result = true;
System.Collections.Generic.Dictionary<SqlTransaction, int> dicTrans = new System.Collections.Generic.Dictionary<SqlTransaction, int>();
System.Collections.Generic.Dictionary<SqlConnection, SqlCommand> diccc = new System.Collections.Generic.Dictionary<SqlConnection, SqlCommand>();
foreach (TransactionScopeModel tsm in list)
{
try
{
SqlConnection conn = new SqlConnection(tsm.conn);
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.Transaction = trans;
cmd.CommandText = tsm.cmdTxt;
if (tsm.param != null)
{
foreach (SqlParameter p in tsm.param)
{
cmd.Parameters.Add(p);
}
}
int rows = cmd.ExecuteNonQuery();
result = rows > 0 ? true : result;
dicTrans.Add(trans, rows);
diccc.Add(conn, cmd);
}
catch (Exception)
{
result = false;
break;
}
}
foreach (int rows in dicTrans.Values)
{
if (rows > 0)
{
continue;
}
else
{
result = false;
break;
}
}
if (result)
{
foreach (SqlTransaction trans in dicTrans.Keys)
{
trans.Commit();
}
}
else
{
foreach (SqlTransaction trans in dicTrans.Keys)
{
trans.Rollback();
}
}
foreach (SqlConnection conn in diccc.Keys)
{
conn.Close();
}
foreach (SqlCommand cmd in diccc.Values)
{
cmd.Dispose();
}
return result;
}
public class TransactionScopeModel
{
public string conn { get; set; }
public string cmdTxt { get; set; }
public SqlParameter[] param { get; set; }
}
#endregion