首页 > 开发 > .Net > 正文

c# 执行事务函数代码

2019-10-27 13:23:16
字体:
来源:转载
供稿:网友

复制代码 代码如下:


/// <summary>
/// 执行多条sql语句,实现事务
/// </summary>
/// <param>多条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;
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表