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

/// <summary>
    /// 执行多条SQL语句(字符串数组形式),实现数据库事务。
    /// </summary>
    /// <param>多条SQL语句</param>   
    /// 调用 Transaction 对象的 Commit 方法来完成事务,或调用 Rollback 方法来取消事务。
    public static int ExecuteTransaction(string[] SQLStringList,int p)
    {
        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 < p; 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
    /// <summary>
    /// 执行存储过程获取所需编号(各表主键)
    /// </summary>
    /// <param>存储过程参数</param>
    /// <param>存储过程参数(默认为1)</param>
    /// <returns>编号(各表主键)</returns>
    public static string Get_FlowNum(string FlowName, int StepLen = 1)
    {
        OracleConnection mycon = new OracleConnection(connectionString);
        try
        {
            mycon.Open();
            OracleCommand MyCommand = new OracleCommand("ALARM_GET_FLOWNUMBER", mycon);
            MyCommand.CommandType = CommandType.StoredProcedure;
            MyCommand.Parameters.Add(new OracleParameter("I_FlowName", OracleType.VarChar, 50));
            MyCommand.Parameters["I_FlowName"].Value = FlowName;
            MyCommand.Parameters.Add(new OracleParameter("I_SeriesNum", OracleType.Number));
            MyCommand.Parameters["I_SeriesNum"].Value = StepLen;
            MyCommand.Parameters.Add(new OracleParameter("O_FlowValue", OracleType.Number));
            MyCommand.Parameters["O_FlowValue"].Direction = ParameterDirection.Output;
            MyCommand.ExecuteNonQuery();
            return MyCommand.Parameters["O_FlowValue"].Value.ToString();
        }
        catch
        {
            return "";
        }
        finally
        {
            mycon.Close();
        }
    }

}

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

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