using System;
using System.Data;
//using System.Data.SqlClient;
//using System.Data.Sql;
using System.Data.Common;
using System.Configuration;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.ObjectBuilder;
using System.Text;
using System.IO;
using System.Web;

public class DataAccess_GestorePDF_NMA
{
    public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<GestorePDF_NMA_Parametro> paramList)
    {

        // The DataTable to be ret  urned 
        Database db = GetDatabaseObject(_dbprovider);

        // Execute the command making sure the connection gets closed in the end
        DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
        //VF imposto il timeout a 120s per vedere se riesce a gestire le connessioni che non si liberano immediatamente
        //comm.CommandTimeout = 120;
        //
        if (paramList != null)
        {
            foreach (GestorePDF_NMA_Parametro param in paramList)
            {

                db.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 = db.ExecuteDataSet(comm).Tables[0];
            table.TableName = "TABLENAME";
            return table;
        }
        catch (Exception ex)
        {
            DataBaseException_GestorePDF_NMA ecc = new DataBaseException_GestorePDF_NMA(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
            throw ecc;
        }
    }
    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.SqlServerReportModeler)
        {
            connstring = WebConfigParameter.getConnectionString("SqlServerConnectionReportModeler");
            db = new SqlDatabase(connstring);
        }
        else if (_dbprovider == DBProvider.Oracle)
        {
            connstring = WebConfigParameter.getConnectionString("OracleConnection");
            db = new OracleDatabase(connstring);
        }

        return db;
    }
}

/// <summary>
/// Derivazione del System.Data.Common.DbParameter:
/// Nel progetto "GestorePDF-NMA"
/// � usato solo dal DataAccess.cs e dal DataBaseException.cs.
/// </summary>
public class GestorePDF_NMA_Parametro : DbParameter
{
    DbType _dbtype;
    ParameterDirection _paramdirection;
    bool _nullable;
    string _paramName;
    int _size;
    string _sourceColumn;
    bool _sourcecolumnnullmapping;
    DataRowVersion _sourceversion;
    object _value;

    public override DbType DbType
    {
        get
        {
            return _dbtype;
        }
        set
        {
            _dbtype = value;
        }
    }
    public override ParameterDirection Direction
    {
        get
        {
            return _paramdirection;
        }
        set
        {
            _paramdirection = value;
        }
    }
    public override bool IsNullable
    {
        get
        {
            return _nullable;
        }
        set
        {
            _nullable = value;
        }
    }
    public override string ParameterName
    {
        get
        {
            return _paramName;
        }
        set
        {
            _paramName = value;
        }
    }
    public override int Size
    {
        get
        {
            return _size;
        }
        set
        {
            _size = value;
        }
    }
    public override string SourceColumn
    {
        get
        {
            return _sourceColumn;
        }
        set
        {
            _sourceColumn = value;
        }
    }
    public override void ResetDbType()
    {
        throw new Exception("The method or operation is not implemented.");
    }
    public override bool SourceColumnNullMapping
    {
        get
        {
            return _sourcecolumnnullmapping;
        }
        set
        {
            _sourcecolumnnullmapping = value;
        }
    }
    public override DataRowVersion SourceVersion
    {
        get
        {
            return _sourceversion;
        }
        set
        {
            _sourceversion = value;
        }
    }
    public override object Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
        }
    }

    public GestorePDF_NMA_Parametro()
    {
        _sourceversion = DataRowVersion.Current;
        _paramdirection = ParameterDirection.Input;
    }
}