private static SqlDataAdapter GetAdapter(string sql, SqlParameter[] parameters) { DALSql dal = GetThreadDal(); dal.CheckConnection(); SqlCommand sc = new SqlCommand(sql, dal.Connection); sc.CommandType = CommandType.Text; sc.CommandTimeout = 0; if (parameters != null) { sc.Parameters.AddRange(parameters); } foreach (SqlParameter parameter in sc.Parameters) { if (parameter.Value == null) { parameter.Value = DBNull.Value; } if (parameter.SqlDbType == SqlDbType.DateTime && parameter.Value.Equals(new DateTime(1, 1, 1))) { parameter.Value = DBNull.Value; } } return(new SqlDataAdapter(sc)); }
public static void CloseConnection() { DALSql dal = GetThreadDal(); if (dal.Connection != null) { dal.Connection.Close(); dal.Connection = null; } }
/// <summary> /// Получает DAL для текущего потока /// </summary> private static DALSql GetThreadDal() { int threadId = Thread.CurrentThread.ManagedThreadId; DALSql dal; Pool.TryGetValue(threadId, out dal); if (dal == null) { dal = new DALSql(); Pool.Add(threadId, dal); } return(dal); }
public static void BulkCopy(string tableName, DataTable insertData) { DALSql dal = GetThreadDal(); dal.CheckConnection(); SqlBulkCopy bulkCopy = new SqlBulkCopy(dal.Connection); bulkCopy.DestinationTableName = tableName; foreach (DataColumn dc in insertData.Columns) { bulkCopy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName); } bulkCopy.WriteToServer(insertData); }
public static void SetConnectionString(string connectionString) { DALSql dal = GetThreadDal(); if (dal.ConnectionString != connectionString) { if (dal.Connection != null) { dal.Connection.Close(); dal.Connection = null; } dal.ConnectionString = connectionString; } }
public static void AddEventText(Action <string> act) { DALSql dal = GetThreadDal(); dal.CheckConnection(); if (dal.EventText != null) { dal.Connection.InfoMessage -= dal.EventText; } dal.EventText = new SqlInfoMessageEventHandler( (sender, e) => { act(e.Message); }); dal.Connection.InfoMessage += dal.EventText; }