事务执行多条sql语句添加

/// <summary>
/// 事务执行多条sql语句添加
/// </summary>
/// <param name="SQLStringList"></param>
/// <returns></returns>
public int ExecuteSqlTran(List<String> SQLStringList)
{
using (SqlConnection conn = new SqlConnection(con))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
int count = 0;
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n];
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch (Exception err)
{
tx.Rollback();

return 0;
}
}

}

posted @ 2020-06-27 14:53  奇怪问题  阅读(388)  评论(0编辑  收藏  举报