using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using DataAccessLayer;

public class DataAccessPA
{
    public static IDataReader ExecuteDataReader(DBProvider _dbprovider, CommandType _commandType, string _commandText)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);

        try
        {
            // Execute the command making sure the connection gets closed in the end
            var result = connection.DataBase.ExecuteReader(_commandType, _commandText);
            connection.Release();
            return result;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, _commandText);
            connection.Dispose();
            throw ecc;
        }
    }

    public static DataTable ExecuteDataTable(DBProvider _dbprovider, CommandType _commandType, string _commandText)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        try
        {
            // Execute the command making sure the connection gets closed in the end
            DataTable table = connection.DataBase.ExecuteDataSet(_commandType, _commandText).Tables[0];
            table.TableName = "TABLENAME";
            connection.Release();
            return table;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, _commandText);
            connection.Dispose();
            throw ecc;
        }

    }

    public static object ExecuteScalar(DBProvider _dbprovider, CommandType _commandType, string _commandText)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        try
        {
            // Execute the command making sure the connection gets closed in the end
            var result = connection.DataBase.ExecuteScalar(_commandType, _commandText);
            connection.Release();
            return result;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, _commandText);
            connection.Dispose();
            throw ecc;
        }
    }

    public static int ExecuteNonQueryStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, SqlParameter[,] paramList)
    {
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        int ritorno = -1;
        SqlConnection con = (SqlConnection)connection.Connection;
        using (SqlTransaction tran = (SqlTransaction)con.BeginTransaction())
        {
            try
            {
                for (int i = 0; i < paramList.GetLength(0); i++)
                {
                    using (SqlCommand cmd = new SqlCommand(nomeStoredProcedure, con, tran))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        for (int j = 0; j < paramList.GetLength(1); j++)
                        {
                            cmd.Parameters.Add(paramList[i, j]);
                        }
                        ritorno = cmd.ExecuteNonQuery();
                    }
                }
                tran.Commit();
            }
            catch
            {
                ritorno = -2;
                tran.Rollback();
                connection.Dispose();
                return ritorno;
            }
        }
        connection.Release();
        return ritorno;
    }


    public static int ExecuteNonQuery(DBProvider _dbprovider, CommandType _commandType, string _commandText)
    {
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        int ritorno = -1;
        SqlConnection con = (SqlConnection)connection.Connection;

        using (SqlTransaction tran = con.BeginTransaction())
        {
            using (SqlCommand cmd = new SqlCommand(_commandText, con, tran))
            {
                try
                {
                    ritorno = cmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    connection.Dispose();
                    ritorno = -2;
                    return ritorno;
                }
                finally
                {

                }

            }
        }
        connection.Release();
        return ritorno;
    }

    //    using (DbConnection connection = db.CreateConnection())
    //    {
    //        connection.Open();
    //        DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Serializable);
    //        try
    //        {
    //            DbCommand comm = null;
    //            if (_commandType == CommandType.StoredProcedure)
    //            {
    //                comm = db.GetStoredProcCommand(_commandText);
    //            }
    //            else
    //            {
    //                comm = db.GetSqlStringCommand(_commandText);
    //            }


    //            Parametro returnParam = null;

    //            if (_dbprovider != DBProvider.Oracle)
    //            {
    //                returnParam = new Parametro();
    //                returnParam.ParameterName = "RETURN";
    //                returnParam.DbType = DbType.Int32;
    //                returnParam.Direction = ParameterDirection.ReturnValue;
    //                returnParam.Value = -1;
    //                db.AddParameter(comm, returnParam.ParameterName, returnParam.DbType, returnParam.Direction, returnParam.SourceColumn, returnParam.SourceVersion, returnParam.Value);
    //            }
    //            ritorno = db.ExecuteNonQuery(comm, transaction);

    //            if (_dbprovider != DBProvider.Oracle)
    //                ritorno = (Int32)comm.Parameters["@RETURN"].Value;


    //            //Commit the transaction
    //            transaction.Commit();
    //        }
    //        catch (Exception ex)
    //        {
    //            //Roll back the transaction. 
    //            transaction.Rollback();
    //            DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, _commandText);
    //            throw ecc;
    //        }
    //    }
    //    return ritorno;
    //}

    public static IDataReader ExecuteDataReaderStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);

        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);
        comm.CommandTimeout = 120;
        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }

        try
        {
            // Execute the command making sure the connection gets closed in the end
            var result = connection.DataBase.ExecuteReader(comm);
            connection.Release();
            return result;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
            connection.Dispose();
            throw ecc;
        }
    }

    public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);
        comm.CommandType = CommandType.StoredProcedure;

        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }

        try
        {
            // Execute the command making sure the connection gets closed in the end
            DataTable table = connection.DataBase.ExecuteDataSet(comm).Tables[0];
            table.TableName = "TABLENAME";
            connection.Release();
            return table;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
            connection.Dispose();
            throw ecc;
        }
    }

    public static DataSet ExecuteDataSetStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);

        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }

        try
        {
            // Execute the command making sure the connection gets closed in the end
            DataSet ds = connection.DataBase.ExecuteDataSet(comm);
            connection.Release();
            return ds;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
            connection.Dispose();
            throw ecc;
        }
    }

    public static DataSet ExecuteDataSetStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramListInput, List<string> paramNameOutputList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);

        if (paramListInput != null)
        {
            foreach (Parametro param in paramListInput)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }

        foreach (string paramOut in paramNameOutputList)
        {
            comm.Parameters.Add(AddRefCursorParameter(paramOut));
        }

        try
        {
            // Execute the command making sure the connection gets closed in the end
            DataSet dataset = connection.DataBase.ExecuteDataSet(comm);
            connection.Release();
            return dataset;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramListInput);
            connection.Dispose();
            throw ecc;
        }
    }

    /// <summary>
    /// Lancia la stored il cui nome � nomeStoredProcedure con in parametri passati in input.
    /// Viene aggiunto automaticamente un parametro in output il cui nome � paramNameOutput che contiene il resulset della stored procedure.
    /// Utilizzare solo per il provider Oracle.
    /// </summary>
    /// <param name="_dbprovider">Provider verso il quale lanciare la stored</param>
    /// <param name="nomeStoredProcedure">Nome della stored procedure da lanciare</param>
    /// <param name="paramListInput">Lista di parametri in input</param>
    /// <param name="paramNameOutput">Nome del parametro in output</param>
    /// <returns></returns>
    public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramListInput, string paramNameOutput)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);

        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);

        if (paramListInput != null)
        {
            foreach (Parametro param in paramListInput)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }

            comm.Parameters.Add(AddRefCursorParameter(paramNameOutput));
        }

        try
        {
            // Execute the command making sure the connection gets closed in the end
            DataTable table = connection.DataBase.ExecuteDataSet(comm).Tables[0];
            table.TableName = "TABLENAME";
            connection.Release();
            return table;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramListInput);
            connection.Dispose();
            throw ecc;
        }
    }

    /// <summary>
    /// Lancia la stored il cui nome � nomeStoredProcedure con in parametri passati in input.
    /// Oltre ai classici parametri di input pu� accettare un Datatable che verr� mappato come cursore 
    /// Utilizzare solo per il provider Oracle.
    /// </summary>
    /// <param name="_dbprovider">Provider verso il quale lanciare la stored</param>
    /// <param name="nomeStoredProcedure">Nome della stored procedure da lanciare</param>
    /// <param name="paramListInput">Lista di parametri in input</param>
    /// <param name="cursorParamList">Lista di parametri Cursore(DataTable) di input o di output</param>
    /// <returns></returns>
    public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramListInput, List<OracleParameter> cursorParamList, OracleParameter outParam)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);
        if (paramListInput != null)
        {
            foreach (Parametro param in paramListInput)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }
        if (cursorParamList != null) //Khorne
        {
            foreach (OracleParameter oraParam in cursorParamList)
            {
                // creation of cursor parameters IN and OUT
                comm.Parameters.Add(oraParam);
            }
        }
        //parametro di output
        comm.Parameters.Add(outParam);
        try
        {
            // Execute the command making sure the connection gets closed in the end
            DataTable table = connection.DataBase.ExecuteDataSet(comm).Tables[0];
            table.TableName = "TABLENAME";
            connection.Release();
            return table;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramListInput);
            connection.Dispose();
            throw ecc;
        }
    }

    public static object ExecuteScalarStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);
        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }
        try
        {
            // Execute the command making sure the connection gets closed in the end
            var result = connection.DataBase.ExecuteScalar(comm);
            connection.Release();
            return result;
        }
        catch (Exception ex)
        {
            DataBaseException ecc = new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
            connection.Dispose();
            throw ecc;
        }
    }

    public static int ExecuteNonQueryStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        int ritorno = -1;
        SqlConnection con = (SqlConnection)connection.Connection;

        DbTransaction transaction = con.BeginTransaction(IsolationLevel.Serializable);
        try
        {
            // Execute the command making sure the connection gets closed in the end
            DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);
            if (paramList != null)
            {
                foreach (Parametro param in paramList)
                {
                    connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
                }
            }
            Parametro returnParam = null;

            if (_dbprovider != DBProvider.Oracle)
            {
                returnParam = new Parametro();
                returnParam.ParameterName = "RETURN";
                returnParam.DbType = DbType.Int32;
                returnParam.Direction = ParameterDirection.ReturnValue;
                returnParam.Value = -1;

                connection.DataBase.AddParameter(comm, returnParam.ParameterName, returnParam.DbType, returnParam.Direction, returnParam.SourceColumn, returnParam.SourceVersion, returnParam.Value);
            }

            ritorno = connection.DataBase.ExecuteNonQuery(comm, transaction);

            if (_dbprovider != DBProvider.Oracle)
                ritorno = (Int32)comm.Parameters["@RETURN"].Value;
            //transaction.IsolationLevel = IsolationLevel.
            //Commit the transaction
            transaction.Commit();
            connection.Release();
            return ritorno;

        }
        catch (DataBaseException ex)
        {
            //Roll back the transaction. 
            transaction.Rollback();
            connection.Dispose();
            throw new DataBaseException(ex, connection.DataBase.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
        }

    }

    /// <summary>
    /// Usata solamente per lanciare la procedura avvio_report
    /// </summary>
    /// <param name="_dbprovider"></param>
    /// <param name="nomeStoredProcedure"></param>
    /// <param name="paramList"></param>
    /// <returns></returns>
    public static int ExecuteNonQueryStoredProcedureNoTransaction(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        int ritorno = -1;

        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetStoredProcCommand(nomeStoredProcedure);
        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }

        try
        {
            // Execute the command making sure the connection gets closed in the end
            connection.DataBase.ExecuteNonQuery(comm);
            ritorno = Convert.ToInt32(comm.Parameters["v_RIS"].Value);
            connection.Release();
            return ritorno;
        }
        catch (Exception ex)
        {
            string msg = connection.DataBase.ConnectionStringWithoutCredentials;
            connection.Dispose();
            throw new DataBaseException(ex, msg, nomeStoredProcedure, paramList);
        }
    }

    public static IDataReader ExecuteDataReaderSqlStatement(DBProvider _dbprovider, string sqlText, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetSqlStringCommand(sqlText);
        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }
        try
        {
            var result = connection.DataBase.ExecuteReader(comm);
            connection.Release();
            return result;
        }
        catch (Exception ex)
        {
            string msg = connection.DataBase.ConnectionStringWithoutCredentials;
            connection.Dispose();
            throw new DataBaseException(ex, msg, sqlText, paramList);
        }
    }

    public static DataTable ExecuteDataTableSqlStatement(DBProvider _dbprovider, string sqlText, List<Parametro> paramList)
    {
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetSqlStringCommand(sqlText);
        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {
                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }
        try
        {
            DataTable table = connection.DataBase.ExecuteDataSet(comm).Tables[0];
            table.TableName = "TABLENAME";
            connection.Release();
            return table;
        }
        catch (Exception ex)
        {
            string msg = connection.DataBase.ConnectionStringWithoutCredentials;
            connection.Dispose();
            throw new DataBaseException(ex, msg, sqlText, paramList);
        }


    }

    public static object ExecuteScalarSqlStatement(DBProvider _dbprovider, string sqlText, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);

        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = connection.DataBase.GetSqlStringCommand(sqlText);
        if (paramList != null)
        {
            foreach (Parametro param in paramList)
            {

                connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
            }
        }
        try
        {
            var result = connection.DataBase.ExecuteScalar(comm);
            connection.Release();
            return result;
        }
        catch (Exception ex)
        {
            string msg = connection.DataBase.ConnectionStringWithoutCredentials;
            connection.Dispose();
            throw new DataBaseException(ex, msg, sqlText, paramList);
        }
    }

    public static int ExecuteNonQuerySqlStatement(DBProvider _dbprovider, string sqlText, List<Parametro> paramList)
    {
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        int ritorno = -1;
        SqlConnection con = (SqlConnection)connection.Connection;

        DbTransaction transaction = con.BeginTransaction(IsolationLevel.Serializable);
        try
        {
            // Execute the command making sure the connection gets closed in the end
            DbCommand comm = connection.DataBase.GetSqlStringCommand(sqlText);
            if (paramList != null)
            {
                foreach (Parametro param in paramList)
                {

                    connection.DataBase.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
                }
            }
            Parametro returnParam = null;

            if (_dbprovider != DBProvider.Oracle)
            {
                returnParam = new Parametro();
                returnParam.ParameterName = "RETURN";
                returnParam.DbType = DbType.Int32;
                returnParam.Direction = ParameterDirection.ReturnValue;
                returnParam.Value = -2;

                connection.DataBase.AddParameter(comm, returnParam.ParameterName, returnParam.DbType, returnParam.Direction, returnParam.SourceColumn, returnParam.SourceVersion, returnParam.Value);
            }

            ritorno = connection.DataBase.ExecuteNonQuery(comm, transaction);

            if (_dbprovider != DBProvider.Oracle)
                ritorno = (Int32)comm.Parameters["@RETURN"].Value;


            //Commit the transaction
            transaction.Commit();
            connection.Release();
        }
        catch (Exception ex)
        {
            //Roll back the transaction. 
            transaction.Rollback();
            string msg = connection.DataBase.ConnectionStringWithoutCredentials;
            connection.Dispose();
            throw new DataBaseException(ex, msg, sqlText, paramList);
        }

        return ritorno;
    }

    public static bool ExecuteNonQueryMultipleTransaction(DBProvider _dbprovider, List<TransactionStatement> _transactionStatement)
    {
        bool result = true;
        string _sqlstatement = "";
        List<Parametro> _paramlist = null;
        DbCommand command = null;
        // The DataTable to be ret  urned 
        //Database db = GetDatabaseObject(_dbprovider);
        SingleConnection connection = ConnectionPool.GetDataBase(_dbprovider);
        SqlConnection con = (SqlConnection)connection.Connection;
        DbTransaction transaction = con.BeginTransaction(IsolationLevel.Serializable);

        try
        {
            foreach (TransactionStatement _trans in _transactionStatement)
            {
                _sqlstatement = _trans.SqlStatement;
                _paramlist = _trans.ListaParametro;
                // Esegue la transazione.

                if (_trans.StatementType == CommandType.Text)
                    command = connection.DataBase.GetSqlStringCommand(_trans.SqlStatement);
                else if (_trans.StatementType == CommandType.StoredProcedure)
                    command = connection.DataBase.GetStoredProcCommand(_trans.SqlStatement);

                foreach (Parametro param in _trans.ListaParametro)
                    connection.DataBase.AddParameter(command, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
                    
                connection.DataBase.ExecuteNonQuery(command, transaction);
            }
            // Commit the transaction.
            transaction.Commit();
        }
        catch (Exception ex)
        {
            // Roll back the transaction. 
            transaction.Rollback();
            string msg = connection.DataBase.ConnectionStringWithoutCredentials;
            DataBaseException ecc = new DataBaseException(ex, msg, _sqlstatement, _paramlist);
            throw ecc;
        }
        finally
        {
            connection.Release();
        }

        return result;

    }


    private static string GetConnectionString(DBProvider _dbprovider)
    {
        switch (_dbprovider)
        {
            case DBProvider.SqlServer:
                return WebConfigParameter.getConnectionString("SqlServerConnection");
            case DBProvider.SqlServerStampeC6:
                return WebConfigParameter.getConnectionString("SqlServerStampeC6Connection");
            case DBProvider.SqlServerConsulenzaBase:
                return WebConfigParameter.getConnectionString("SqlServerConnectionConsulenzaBase");
            case DBProvider.SqlServerConsulenzaUnica:
                return WebConfigParameter.getConnectionString("SqlServerConsulenzaUnicaConnection");
            case DBProvider.SqlServerConsulenzaEvoluta:
                return WebConfigParameter.getConnectionString("SqlServerConsulenzaEvolutaConnection");
            case DBProvider.SqlServerCatalogoProdotti:
                return WebConfigParameter.getConnectionString("SqlServerCatalogoProdottiConnection");
            case DBProvider.SqlServerReportModeler:
                return WebConfigParameter.getConnectionString("SqlServerConnectionReportModeler");
            case DBProvider.SqlServerWebTemplateReportModeler:
                return WebConfigParameter.getConnectionString("SqlServerConnectionWebTemplateReportModeler");
            case DBProvider.SqlServerReportArchive:
                return WebConfigParameter.getConnectionString("SqlServerConnectionReportArchive");
            case DBProvider.Oracle:
                return WebConfigParameter.getConnectionString("OracleConnection");
            default:
                return "";
        }
    }

    private static Database GetDatabaseObject(DBProvider _dbprovider)
    {
        // The DataTable to be ret  urned 
        Database db = null;
        string connstring;

        if (_dbprovider == DBProvider.SqlServer)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConnection");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerStampeC6)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerStampeC6Connection");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerConsulenzaBase)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConnectionConsulenzaBase");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerConsulenzaUnica)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConsulenzaUnicaConnection");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerConsulenzaEvoluta)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConsulenzaEvolutaConnection");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerCatalogoProdotti)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerCatalogoProdottiConnection");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerReportModeler)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConnectionReportModeler");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerWebTemplateReportModeler)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConnectionWebTemplateReportModeler");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.SqlServerReportArchive)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConnectionReportArchive");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.Oracle)
        {
            connstring = WebConfigParameter.getConnectionString("OracleConnection");
            db = new OracleDatabase(connstring);
        }

        return db;
    }

    private static OracleParameter AddRefCursorParameter(string name)
    {
        return new OracleParameter(name, OracleType.Cursor, 0, ParameterDirection.Output, true, 0, 0, String.Empty, DataRowVersion.Default, DBNull.Value);
    }
}