asp.net Oracle数据库访问操作类(4)

/// <summary>
    /// 执行多条SQL语句(list的形式),实现数据库事务。
    /// </summary>
    /// <param>多条SQL语句</param>   
    /// 调用 Transaction 对象的 Commit 方法来完成事务,或调用 Rollback 方法来取消事务。
    public static int ExecuteSqlTran(List<String> SQLStringList)
    {
        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            connection.Open();
            // 为事务创建一个命令
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = connection;
            OracleTransaction tx = connection.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();//用Commit方法来完成事务
                return count;//
            }
            catch
            {
                tx.Rollback();//出现错误,事务回滚!
                return 0;
            }
            finally
            {
                cmd.Dispose();
                connection.Close();//关闭连接
            }
        }
    }
    #endregion
    #region 事务处理

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgppz.html