/// <summary>
/// 执行多条sql语句,实现事务
/// </summary>
/// <param name="arraySql">多条sql语句</param>
public int ExecutrSqlTran(System.Collections.ArrayList arraySql)
{
int itemnum;
DbOpen();
SqlCommand cm = new SqlCommand();
cm.Connection = scn;
SqlTransaction tx = scn.BeginTransaction();
cm.Transaction = tx;
try
{
for (int i = 0; i < arraySql.Count; i++)
{
string strSql = arraySql[i].ToString();
if (strSql.Trim().Length > 1)
{
cm.CommandText = strSql;
cm.ExecuteNonQuery();
}
}
tx.Commit();
itemnum = 1;
}
catch (SqlException E)
{
tx.Rollback();
itemnum = 0;
throw new Exception(E.Message);
}
DbClose();
return itemnum;
}
}
c#执行事务
本文介绍了一个使用C#实现的方法,该方法能够通过事务处理来批量执行SQL语句。具体包括开启数据库连接、创建并开始事务、执行每一条SQL语句,并在所有语句成功后提交事务,若任一语句执行失败则回滚事务。


被折叠的 条评论
为什么被折叠?



