好像只能应用于SQLServer,其它数据库未做测试。
using System.Data;
using System.Data.SqlClient;
???????? /// <summary>
???????? /// 执行多条SQL语句,实现数据库事务。
????? /// </summary>
??????? /// <param name="SQLStringList">多条SQL语句</param>??
??????? public int ExecuteSqlTran(ArrayList SQLStringList)
??????? {
??????????? int results = 0;
??????????? using (SqlConnection conn = new SqlConnection(connectionString))
??????????? {
??????????????? conn.Open();
??????????????? SqlCommand cmd = new SqlCommand();
??????????????? cmd.Connection = conn;
??????????????? SqlTransaction tx = conn.BeginTransaction();
??????????????? cmd.Transaction = tx;
??????????????? try
??????????????? {
??????????????????? for (int n = 0; n < SQLStringList.Count; n++)
??????????????????? {
??????????????????????? string strsql = SQLStringList[n].ToString();
??????????????????????? if (strsql.Trim().Length > 1)
??????????????????????? {
??????????????????????????? cmd.CommandText = strsql;
??????????????????????????? AddSQLLog(strsql);
??????????????????????????? cmd.ExecuteNonQuery();
??????????????????????? }
??????????????????? }
??????????????????? tx.Commit();
??????????????????? results = 1;
??????????????? }
??????????????? catch
??????????????? {
??????????????????? tx.Rollback();
??????????????????? results = 0;
??????????????? }
??????????????? finally
??????????????? {
??????????????????? cmd.Dispose();
??????????????????? conn.Close();
??????????????? }
??????????????? return results;
??????????? }
??????? }
关键字: SqlTransaction 数据库事务
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论