514 lines
16 KiB
C#
514 lines
16 KiB
C#
using System;
|
|
using System.Data;
|
|
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 System.Data.SqlClient;
|
|
using NLog;
|
|
using DataAccessLayer;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Collections.Concurrent;
|
|
using System.Configuration;
|
|
using System.Threading;
|
|
|
|
namespace DataAccessLayer
|
|
{
|
|
public class DataAccess
|
|
{
|
|
|
|
public static DataTable ExecuteDataTableStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
|
{
|
|
Database db = GetDatabaseObject(_dbprovider);
|
|
DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
|
|
if (paramList != null)
|
|
{
|
|
foreach (Parametro param in paramList)
|
|
{
|
|
db.AddParameter(comm, param.ParameterName, param.DbType, param.Direction, param.SourceColumn, param.SourceVersion, param.Value);
|
|
}
|
|
}
|
|
try
|
|
{
|
|
SqlLogging.LogStoredProcedureStart(nomeStoredProcedure, paramList);
|
|
var result = db.ExecuteDataSet(comm).Tables[0];
|
|
SqlLogging.LogStoredProcedureSuccess(nomeStoredProcedure, result.Rows.Count);
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SqlLogging.LogStoredProcedureError(nomeStoredProcedure, ex, paramList);
|
|
DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
|
throw ecc;
|
|
}
|
|
}
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
|
|
public static IDataReader ExecuteDataReaderStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<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);
|
|
comm.CommandTimeout = 120;
|
|
if (paramList != null)
|
|
{
|
|
foreach (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
|
|
return db.ExecuteReader(comm);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//logger.Error(db.ConnectionStringWithoutCredentials);
|
|
//logger.Error(nomeStoredProcedure);
|
|
DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
|
SqlLogging.LogErrors(ex);
|
|
throw ecc;
|
|
}
|
|
}
|
|
|
|
public static object ExecuteScalarStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<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);
|
|
if (paramList != null)
|
|
{
|
|
foreach (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
|
|
return db.ExecuteScalar(comm);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
DataBaseException ecc = new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
|
SqlLogging.LogErrors(ex);
|
|
throw ecc;
|
|
}
|
|
}
|
|
|
|
public static int ExecuteNonQueryStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
|
{
|
|
// The DataTable to be ret urned
|
|
Database db = GetDatabaseObject(_dbprovider);
|
|
|
|
int ritorno = -1;
|
|
|
|
using (DbConnection connection = db.CreateConnection())
|
|
{
|
|
connection.Open();
|
|
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.Serializable);
|
|
try
|
|
{
|
|
// Execute the command making sure the connection gets closed in the end
|
|
DbCommand comm = db.GetStoredProcCommand(nomeStoredProcedure);
|
|
if (paramList != null)
|
|
{
|
|
foreach (Parametro param in paramList)
|
|
{
|
|
db.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;
|
|
|
|
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;
|
|
|
|
transaction.Commit();
|
|
return ritorno;
|
|
|
|
}
|
|
catch (DataBaseException ex)
|
|
{
|
|
|
|
transaction.Rollback();
|
|
SqlLogging.LogErrors(ex);
|
|
//throw new DataBaseException(ex, db.ConnectionStringWithoutCredentials, nomeStoredProcedure, paramList);
|
|
return ritorno;
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|
|
|
|
public class DataAccessDE : IDisposable
|
|
{
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
private static ConcurrentDictionary<string, bool> preCalculatedProcedureTables = new ConcurrentDictionary<string, bool>();
|
|
private static string[] mergedProcedures = new string[] {
|
|
"c6martperiodico.pl_d2_s169rischiomercatorischiocredito",
|
|
"c6martperiodico.pl_d2_s170rischiodiversificazione"
|
|
};
|
|
public DBProvider Provider { get; private set; }
|
|
public DataAccessDE(DBProvider provider)
|
|
{
|
|
//logger.Traces("DE1_CA="+System.Reflection.Assembly.GetCallingAssembly().GetName().Name+"_EA="+System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);//=2=
|
|
Provider = provider;
|
|
}
|
|
public void Dispose(){ }
|
|
public IDataReader ExecuteDataReaderStoredProcedure(DBProvider _dbprovider, string nomeStoredProcedure, List<Parametro> paramList)
|
|
{
|
|
IDataReader reader;
|
|
List<SqlParameter> parameters = new List<SqlParameter>();
|
|
if (paramList != null)
|
|
{
|
|
foreach (Parametro p in paramList)
|
|
{
|
|
parameters.Add(new SqlParameter(p.ParameterName, p.Value));
|
|
}
|
|
}
|
|
try
|
|
{
|
|
var vConn = SQLServer.GetConnection6();
|
|
//using (SqlConnection vConn = new SqlConnection(GetConnectionString(Provider)))
|
|
//{
|
|
// vConn.Open();
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
cmd.CommandTimeout = CACHE.CommandTimeout;
|
|
cmd.Connection = vConn;
|
|
cmd.Parameters.AddRange(parameters.ToArray());
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = nomeStoredProcedure;
|
|
cmd.CommandText = ConvertProcedureToTableStatement(paramList, nomeStoredProcedure);
|
|
reader = cmd.ExecuteReader();
|
|
cmd.Parameters.Clear();
|
|
}
|
|
//vConn.Close();
|
|
return reader;
|
|
//}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
return null;
|
|
}
|
|
private string ConvertProcedureToTableStatement(List<Parametro> listaParametri, string storedProcedure)
|
|
{
|
|
return "wh.p_" + storedProcedure.Substring(storedProcedure.IndexOf('.') + 1, storedProcedure.Length - storedProcedure.IndexOf('.') - 1).ToLower().Replace("[", "").Replace("]", "");
|
|
}
|
|
}
|
|
public enum DBProvider : int
|
|
{
|
|
Oracle,
|
|
SqlServer,
|
|
SqlServerStampeC6,
|
|
|
|
/// <summary>
|
|
/// E' la stringa di connessione al database Sql Server di ReportModeler (Configuratore dei report per il progetto Consulenza).
|
|
/// </summary>
|
|
SqlServerReportModeler,
|
|
|
|
/// <summary>
|
|
/// E' la stringa di connessione al database Sql Server di Consulenza Base.
|
|
/// </summary>
|
|
SqlServerConsulenzaBase,
|
|
|
|
/// <summary>
|
|
/// E' la stringa di connessione al database Sql Server di Consulenza Base.
|
|
/// </summary>
|
|
SqlServerConsulenzaEvoluta,
|
|
|
|
/// <summary>
|
|
/// E' la stringa di connessione al database Sql Server di Catalogo Prodotti.
|
|
/// </summary>
|
|
SqlServerCatalogoProdotti,
|
|
|
|
/// <summary>
|
|
/// Stringa di connessione al database Sql Server che gestisce la modellazione lato web dei template dei report.
|
|
/// </summary>
|
|
SqlServerWebTemplateReportModeler,
|
|
|
|
/// <summary>
|
|
/// Stringa di connessione al database di archiviazione dei report.
|
|
/// </summary>
|
|
SqlServerReportArchive,
|
|
|
|
/// <summary>
|
|
/// E' la stringa di connessione al database Sql Server di ConsulenzaUnica.
|
|
/// </summary>
|
|
SqlServerConsulenzaUnica
|
|
|
|
}
|
|
}
|
|
|
|
public class Parametro : DbParameter
|
|
{
|
|
public Parametro()
|
|
{
|
|
_sourceversion = DataRowVersion.Current;
|
|
_paramdirection = ParameterDirection.Input;
|
|
}
|
|
|
|
DbType _dbtype;
|
|
|
|
public override DbType DbType
|
|
{
|
|
get
|
|
{
|
|
return _dbtype;
|
|
}
|
|
set
|
|
{
|
|
_dbtype = value;
|
|
}
|
|
}
|
|
|
|
ParameterDirection _paramdirection;
|
|
public override ParameterDirection Direction
|
|
{
|
|
get
|
|
{
|
|
return _paramdirection;
|
|
}
|
|
set
|
|
{
|
|
_paramdirection = value;
|
|
}
|
|
}
|
|
|
|
bool _nullable;
|
|
public override bool IsNullable
|
|
{
|
|
get
|
|
{
|
|
return _nullable;
|
|
}
|
|
set
|
|
{
|
|
_nullable = value;
|
|
}
|
|
}
|
|
|
|
string _paramName;
|
|
public override string ParameterName
|
|
{
|
|
get
|
|
{
|
|
return _paramName;
|
|
}
|
|
set
|
|
{
|
|
_paramName = value;
|
|
}
|
|
}
|
|
|
|
public override void ResetDbType()
|
|
{
|
|
throw new Exception("The method or operation is not implemented.");
|
|
}
|
|
|
|
int _size;
|
|
public override int Size
|
|
{
|
|
get
|
|
{
|
|
return _size;
|
|
}
|
|
set
|
|
{
|
|
_size = value;
|
|
}
|
|
}
|
|
|
|
string _sourceColumn;
|
|
public override string SourceColumn
|
|
{
|
|
get
|
|
{
|
|
return _sourceColumn;
|
|
}
|
|
set
|
|
{
|
|
_sourceColumn = value;
|
|
}
|
|
}
|
|
|
|
bool _sourcecolumnnullmapping;
|
|
public override bool SourceColumnNullMapping
|
|
{
|
|
get
|
|
{
|
|
return _sourcecolumnnullmapping;
|
|
}
|
|
set
|
|
{
|
|
_sourcecolumnnullmapping = value;
|
|
}
|
|
}
|
|
|
|
DataRowVersion _sourceversion;
|
|
public override DataRowVersion SourceVersion
|
|
{
|
|
get
|
|
{
|
|
return _sourceversion;
|
|
}
|
|
set
|
|
{
|
|
_sourceversion = value;
|
|
}
|
|
}
|
|
|
|
object _value;
|
|
public override object Value
|
|
{
|
|
get
|
|
{
|
|
return _value;
|
|
}
|
|
set
|
|
{
|
|
_value = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public class TransactionStatement
|
|
{
|
|
private string _sqlstatement;
|
|
private CommandType _statementtype;
|
|
private List<Parametro> _paramlist;
|
|
|
|
public TransactionStatement()
|
|
{
|
|
_sqlstatement = "";
|
|
_paramlist = new List<Parametro>();
|
|
_statementtype = CommandType.Text;
|
|
}
|
|
|
|
public List<Parametro> ListaParametro
|
|
{
|
|
get
|
|
{
|
|
return _paramlist;
|
|
}
|
|
}
|
|
|
|
public string SqlStatement
|
|
{
|
|
get
|
|
{
|
|
return _sqlstatement;
|
|
}
|
|
set
|
|
{
|
|
_sqlstatement = value;
|
|
}
|
|
}
|
|
|
|
public void addParametroToList(Parametro _parametro)
|
|
{
|
|
_paramlist.Add(_parametro);
|
|
}
|
|
|
|
public CommandType StatementType
|
|
{
|
|
get
|
|
{
|
|
return _statementtype;
|
|
}
|
|
set
|
|
{
|
|
_statementtype = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
|