using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.IO; using System.Security.Cryptography; using System; using System.Linq; using System.Data; using NLog; namespace DataAccessLayer { using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using System.Data.SqlClient; using NLog; using System.Runtime.Remoting.Contexts; public class SQLServer : IDisposable { private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); private static string talking_code = string.Empty; public static int SqlCommandTimeout { get; set; } public Exception SqlException { get; set; } public static string ConnectionString { get; set; } public static string ConnectionStringS { get; set; } public bool Sei { get; set; } = true; public SQLServer(bool sei = true) { talking_code = string.Empty; Sei = sei; } public SQLServer(string cf, string rt, bool sei = true) : this() { talking_code = cf + "_" + rt; Sei = sei; } #region six private static SqlConnection Context6 { get; set; } private static void Test6() { try { if (Context6 == null) { Context6 = new SqlConnection(ConnectionString); } } catch (Exception ex) { } try { Context6.Open(); } catch (Exception ex) { } } public static SqlConnection GetConnection6() { Test6(); if (Context6 != null && Context6.State == ConnectionState.Open) return Context6; else return null; } #endregion #region srv private static SqlConnection ContextS { get; set; } private static void TestS() { try { if (ContextS == null) { ContextS = new SqlConnection(ConnectionStringS); } } catch (Exception ex) { } try { ContextS.Open(); } catch (Exception ex) { } } public static SqlConnection GetConnectionS() { TestS(); if (ContextS != null && ContextS.State == ConnectionState.Open) return ContextS; else return null; } #endregion public SqlConnection GetConnection() { if (Sei) return GetConnection6(); else return GetConnectionS(); } public DataTable executeQuery(string Sql, List Params = null, string nomedt = "data", CommandType TipoEsecuzione = CommandType.Text) { if (!string.IsNullOrEmpty(talking_code)) logger.Debugs(talking_code + ":" + Sql); DataTable result = null; if (Sql.Length > 0) { try { //using (SqlConnection vConn = new SqlConnection(ConnectionString)) //{ // vConn.Open(); var vConn = GetConnection(); using (SqlCommand vComm = new SqlCommand()) { vComm.CommandText = Sql; vComm.CommandTimeout = SqlCommandTimeout; vComm.Connection = vConn; vComm.CommandType = TipoEsecuzione; if (Params != null) { foreach (var par in Params) { vComm.Parameters.AddWithValue("@" + par.ParameterName, par.Value); } } using (SqlDataAdapter adpt = new SqlDataAdapter()) { using (DataTable dt = new DataTable(nomedt)) { adpt.SelectCommand = vComm; try { adpt.Fill(dt); result = dt; } catch (Exception ex) { SqlException = ex; } finally { if (vComm != null && vComm.Parameters != null) vComm.Parameters.Clear(); } } } vComm.Parameters.Clear(); } //vConn.Close(); //} } catch (Exception ex) { SqlException = ex; } } return result; } public DataSet executeProcedure(string Sql, List Params, string nomeds = "data") { if (!string.IsNullOrEmpty(talking_code)) logger.Debugs(talking_code + ":" + Sql); DataSet result = null; try { //using (SqlConnection vConn = new SqlConnection(ConnectionString)) //{ // vConn.Open(); var vConn = GetConnection(); using (SqlCommand vComm = new SqlCommand()) { vComm.CommandText = Sql; vComm.CommandTimeout = SqlCommandTimeout; vComm.Connection = vConn; vComm.CommandType = CommandType.StoredProcedure; if (Params != null) { foreach (var par in Params) { vComm.Parameters.AddWithValue("@" + par.ParameterName, par.Value); } } using (SqlDataAdapter adpt = new SqlDataAdapter()) { using (DataSet ds = new DataSet(nomeds)) { adpt.SelectCommand = vComm; try { adpt.Fill(ds); result = ds; } catch (Exception ex) { SqlException = ex; } finally { if (vComm != null && vComm.Parameters != null) vComm.Parameters.Clear(); } } } vComm.Parameters.Clear(); } //vConn.Close(); //} } catch (Exception ex) { SqlException = ex; } return result; } public DataTable GetDataTableFromProcedure(string Sql, List Params = null, string nomedt = "data") { if (!string.IsNullOrEmpty(talking_code)) logger.Debugs(talking_code + ":" + Sql); DataTable result = null; try { //using (SqlConnection vConn = new SqlConnection(ConnectionString)) //{ //vConn.Open(); var vConn = GetConnection(); using (SqlCommand vComm = new SqlCommand()) { vComm.CommandText = Sql; vComm.CommandTimeout = SqlCommandTimeout; vComm.Connection = vConn; vComm.CommandType = CommandType.StoredProcedure; if (Params != null) { foreach (var par in Params) { vComm.Parameters.AddWithValue("@" + par.ParameterName, par.Value); } } using (SqlDataAdapter adpt = new SqlDataAdapter()) { using (DataTable dt = new DataTable(nomedt)) { adpt.SelectCommand = vComm; try { adpt.Fill(dt); result = dt; } catch (Exception et) { SqlException = et; } finally { if (vComm != null && vComm.Parameters != null) vComm.Parameters.Clear(); } } } } //vConn.Close(); //} } catch (Exception ex) { SqlException = ex; } return result; } public Int32 executeScalarInt(string sql, List Params = null, CommandType cmdtype= CommandType.StoredProcedure) { return executeScalar(sql, Params, cmdtype).ToInt32(); } public string executeScalarStr(string sql, List Params = null, CommandType cmdtype = CommandType.StoredProcedure) { return executeScalar(sql, Params, cmdtype).TOString(); } public object executeScalar(string Sql, List Params = null, CommandType cmdtype = CommandType.StoredProcedure) { if (!string.IsNullOrEmpty(talking_code)) logger.Debugs(talking_code + ":" + Sql); object result = null; try { //using (SqlConnection vConn = new SqlConnection(ConnectionString)) //{ // vConn.Open(); var vConn = GetConnection(); using (SqlCommand vComm = new SqlCommand()) { vComm.CommandText = Sql; vComm.CommandTimeout = SqlCommandTimeout; vComm.Connection = vConn; vComm.CommandType= cmdtype; if (Params != null) { foreach(var par in Params) { vComm.Parameters.AddWithValue("@"+par.ParameterName,par.Value); } } try { result = vComm.ExecuteScalar(); } catch (Exception ex) { SqlException = ex; } finally { if (vComm != null && vComm.Parameters != null) vComm.Parameters.Clear(); } vComm.Parameters.Clear(); } //vConn.Close(); //} } catch (Exception ex) { SqlException = ex; } return result; } public object executeScalarWT(string Sql, List Params = null) { if (!string.IsNullOrEmpty(talking_code)) logger.Debugs(talking_code + ":" + Sql); object result = null; try { //using (SqlConnection vConn = new SqlConnection(ConnectionString)) //{ // vConn.Open(); var vConn = GetConnection(); using (SqlCommand vComm = new SqlCommand()) { vComm.CommandText = Sql; vComm.CommandTimeout = SqlCommandTimeout; vComm.Connection = vConn; vComm.CommandType = CommandType.StoredProcedure; if (Params != null) { foreach (var par in Params) { vComm.Parameters.AddWithValue("@" + par.ParameterName, par.Value); } } try { result = vComm.ExecuteScalar(); } catch (Exception ex) { SqlException = ex; } finally { if (vComm != null && vComm.Parameters != null) vComm.Parameters.Clear(); } vComm.Parameters.Clear(); } // vConn.Close(); //} } catch (Exception ex) { SqlException = ex; } return result; } public Int32 executeNonQueryWT(string Sql, List Params = null, CommandType TipoEsecuzione = CommandType.StoredProcedure) { if (!string.IsNullOrEmpty(talking_code)) logger.Debugs(talking_code + ":" + Sql); Int32 result = 0; try { //using (SqlConnection vConn = new SqlConnection(ConnectionString)) //{ // vConn.Open(); var vConn = GetConnection(); using (SqlCommand vComm = new SqlCommand()) { vComm.CommandText = Sql; vComm.CommandTimeout = SqlCommandTimeout; vComm.Connection = vConn; vComm.CommandType = TipoEsecuzione; if (Params != null) { foreach (var par in Params) { vComm.Parameters.AddWithValue("@" + par.ParameterName, par.Value); } } try { result = vComm.ExecuteNonQuery(); } catch (Exception ex) { SqlException = ex; } finally { if (vComm != null && vComm.Parameters != null) vComm.Parameters.Clear(); } vComm.Parameters.Clear(); } //vConn.Close(); //} } catch (Exception ex) { SqlException = ex; } finally { if (result != 0) result = -1; logger.Trace("SqlServer.Int32::executeNonQueryWT(=" + Sql + "=)"); } return result;//se -1 è tutto ok! } public void Dispose() { } public static void CLOSECONNECTIONS() { try { if (Context6 != null) { Context6.Close(); logger.Traces("SqlServer_CLOSECONNECTION_6_1"); } } catch (Exception ex) { } try { if (Context6 != null) { Context6.Dispose(); Context6 = null; logger.Traces("SqlServer_CLOSECONNECTION_6_2"); } } catch (Exception ex) { } try { if (ContextS != null) { ContextS.Close(); logger.Traces("SqlServer_CLOSECONNECTION_S_1"); } } catch (Exception ex) { } try { if (ContextS != null) { ContextS.Dispose(); ContextS = null; logger.Traces("SqlServer_CLOSECONNECTION_S_2"); } } catch (Exception ex) { } } } }