private static DataBaseProvider _provider; // класс реализующий сервисные функции работы с базой данных /// <summary> /// Выполнить инициалтзацию базы данных /// </summary> internal static void Initialize() { state = DataBaseState.Default; s_slim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); parameters = new DataBaseParameters(1024); }
private DataBaseStructure structure = null; // хранить структуру БД #endregion Fields #region Constructors /// <summary> /// Инициализирует новый экземпляр класса /// </summary> public DataBase(DataBaseAdapter adap) { adapter = adap; structure = new DataBaseStructure(); parameters = new DataBaseParameters(1024); saver = new DataBaseSaver(adapter, parameters); }
private SaverState state = SaverState.Stopped; // текущее состояние /// <summary> /// Инициализирует новый класс /// </summary> /// <param name="Adapted">Адаптер БД</param> public DataBaseSaver(DataBaseAdapter Adapted, DataBaseParameters Parameters) { adapter = Adapted; parameters = Parameters; in_out_mutex = new Mutex(); callBackMutex = new Mutex(); input = new List <DataBaseParameterValue>(); output = new List <DataBaseParameterValue>(); callBackMutex = new Mutex(); timer = new Timer(TimerCallback, null, Timeout.Infinite, timeToBuffer); mutex = new Mutex(); }
/// <summary> /// Инициализировать /// </summary> /// <param name="adap">Адаптер БД</param> /// <param name="parames">Параметры загруженные из БД</param> internal void Inialize(DataBaseAdapter adap, DataBaseParameters parames) { adapter = adap; parameters = parames; }
private int timeToBuffer = 1000; // частота сохранения параметров в БД #endregion Fields #region Constructors /// <summary> /// Инициализирует новый класс /// </summary> /// <param name="Adapted">Адаптер БД</param> public DataBaseSaver(DataBaseAdapter Adapted, DataBaseParameters Parameters) { adapter = Adapted; parameters = Parameters; in_out_mutex = new Mutex(); callBackMutex = new Mutex(); input = new List<DataBaseParameterValue>(); output = new List<DataBaseParameterValue>(); callBackMutex = new Mutex(); timer = new Timer(TimerCallback, null, Timeout.Infinite, timeToBuffer); mutex = new Mutex(); }
/// <summary> /// Закрыть БД /// </summary> public void CloseDB() { SqlConnection.ClearAllPools(); if (saver != null) { saver.Stop(); } parameters = null; }
/// <summary> /// Загружает БД /// </summary> public void LoadDB() { SqlConnection connection = null; try { connection = new SqlConnection(adapter.ConnectionString); connection.Open(); if (connection.State == ConnectionState.Open) { SqlCommand command = new SqlCommand(structure.MainTable.SqlQueryForSelectAll, connection); SqlDataReader result = command.ExecuteReader(); if (result != null) { if (result.IsClosed == false) { //parameters = new DataBaseParameters(1024); while (result.Read()) { DataBaseParameter db_parameter = new DataBaseParameter(); db_parameter.ID = result.GetInt32(structure.MainTable[0].IndexInTable); db_parameter.Created = DateTime.FromOADate(result.GetDouble(structure.MainTable[1].IndexInTable)); db_parameter.Numbe_Prm = result.GetInt32(structure.MainTable[2].IndexInTable); db_parameter.tblHistory = result.GetString(structure.MainTable[3].IndexInTable); db_parameter.tblValues = result.GetString(structure.MainTable[4].IndexInTable); try { db_parameter.Identifier = new Guid(result.GetString(structure.MainTable[5].IndexInTable)); } catch { } ParameterDescriptionLoad(db_parameter); if (parameters == null) { parameters = new DataBaseParameters(1024); } parameters.Insert(db_parameter); //saver.Inialize(adapter, parameters); } } } } } catch (Exception ex) { //ErrorHandler.WriteToLog(this, new ErrorArgs(ex.Message, ErrorType.NotFatal)); throw new Exception(ex.Message, ex); } finally { if (connection != null) { if (connection.State == ConnectionState.Open) { connection.Close(); } connection.Dispose(); } } }
/// <summary> /// Добавить параметр в БД /// </summary> /// <param name="parameter">Параметр, который необходимо добавить</param> public void Insert(DataBaseParameter parameter) { SqlConnection connection = null; SqlTransaction transaction = null; try { connection = new SqlConnection(adapter.ConnectionString); connection.Open(); if (connection.State == ConnectionState.Open) { if (Peek(parameter.Identifier) == false) { if (Peek(parameter.ID) == false) { transaction = connection.BeginTransaction(); CreateParameterInMainTable(connection, transaction, parameter); CreateParameterHistoryTable(connection, transaction, parameter); CreateParameterValuesTable(connection, transaction, parameter); transaction.Commit(); if (parameters == null) { parameters = new DataBaseParameters(1024); } parameters.Insert(parameter); //saver.Inialize(adapter, parameters); } else throw new Exception("Параметр с указанным номером существует в БД."); } else throw new Exception("Параметр с указанным Guid существует в БД"); } else throw new Exception("Не удалось подключиться к БД."); } catch (Exception ex) { try { transaction.Rollback(); } catch (Exception ex1) { throw new Exception(ex1.Message, ex1); } throw new Exception(ex.Message, ex); } finally { if (connection != null) { if (connection.State == ConnectionState.Open) { connection.Close(); } connection.Dispose(); } if (transaction != null) transaction.Dispose(); } }
/// <summary> /// Выполнить инициалтзацию базы данных /// </summary> internal static void Initialize() { state = DataBaseState.Default; s_slim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); parameters = new DataBaseParameters(1024); }