事务处理公共方法
数据层缺一个事务处理的公共方法,哪位手头的SQLHelper有此方法望贴出供参考,要求复用性高。
我想来想去只想出这样:
/// <summary>
/// 事务处理
/// </summary>
/// <param name= "cmdArray "> SqlCommand数组 </param>
protected void ExecuteTransaction(SqlCommand[] cmdArray)
{
//...分解数组作为一个事务执行
}
数据层做好了以后不想再改了,怕考虑不全,请大家给点建议。
------解决方案--------------------/// <summary>
/// 执行事务、传入SQL语句字符串数组
/// </summary>
/// <param name= "strTrans "> </param>
/// <returns> </returns>
public static int ExecuteTransAction(string[] strTrans)
{
using (System.Data.SqlServerCe.SqlCeConnection conn = new System.Data.SqlServerCe.SqlCeConnection(Common.SqlDB.SqlCeDbConnectionString))
{
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
}
System.Data.SqlServerCe.SqlCeCommand cmd = new SqlCeCommand();
// System.Data.OleDb.OleDbTransaction trans =conn.BeginTransaction;
int i = strTrans.Length;
try
{
cmd.Connection = conn;
cmd.Transaction = conn.BeginTransaction();
foreach (string str in strTrans)
{
cmd.CommandText = str;
cmd.ExecuteNonQuery();
}
cmd.Transaction.Commit();
return 1;
}
catch (System.Data.SqlServerCe.SqlCeException ee)
{
cmd.Transaction.Rollback();
throw new Exception(ee.Message);
}
}
}