public static void UpdateDb(ConnectionFactory connectionFactory) { Assembly assembly = Assembly.GetAssembly(typeof(Geomethod.GeoLib.GLib)); UpdateScripts.UpdateDb(connectionFactory, assembly, "Geomethod.GeoLib.Resources.UpdateScripts.sql"); }
public void InitDatabase() { try { using var connection = InitConnection(); if (File.Exists(database_filename) == false) { var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "SolRIA SAFT"); if (Directory.Exists(folder) == false) { Directory.CreateDirectory(folder); } FileStream fs = File.Create(database_filename); fs.Close(); } var versionTable = connection.QueryFirstOrDefault <int>( "select COUNT(1) from sqlite_master where type = 'table' and name = 'DatabaseVersion'"); if (versionTable <= 0) { //no table available do the full create string sqlContent = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "SQL", "v001.txt")); string[] sqlCommands = sqlContent.Split(';'); if (connection.State != ConnectionState.Open) { connection.Open(); } //create the tables using var transaction = connection.BeginTransaction(); foreach (var sql in sqlCommands) { if (string.IsNullOrEmpty(sql)) { continue; } System.Diagnostics.Debug.WriteLine($"sql update command: {sql}"); connection.Execute(sql, transaction: transaction); } transaction.Commit(); } //check for updates on the database var versionDB = connection.QueryFirstOrDefault <int>( "SELECT Version FROM DatabaseVersion ORDER BY Version DESC LIMIT 1;"); if (versionDB < UpdateScripts.Version) { //the database needs to update var updateScripts = UpdateScripts.GetUpdateScripts(versionDB); if (connection.State != ConnectionState.Open) { connection.Open(); } var transaction = connection.BeginTransaction(); //update the database schema and data if (updateScripts != null && updateScripts.Length > 0) { foreach (var script in updateScripts.OrderBy(s => s.Version).ThenBy(s => s.Order)) { connection.Execute(sql: script.Sql, transaction: transaction); } } //update the database version connection.Execute( "INSERT INTO DatabaseVersion (UpgradeDate,Version,AppVersion) VALUES (@UpgradeDate,@Version,@AppVersion);", new { UpdateScripts.Version, UpgradeDate = DateTime.Now, AppVersion = app_version }, transaction); transaction.Commit(); } } catch (Exception ex) { LogException(ex, "InitDatabase"); } }
bool InitInstance() { if (App.instance == null) { Log.LogSystem.LogSystemFlags = LogSystemFlags.UseIp | LogSystemFlags.UseUserId; Log.LogSystem.AddLogHandlers(new MessageFormLogInformer()); } using (WaitCursor wc = new WaitCursor()) { if (App.instance != null) { throw new HospitalDepartmentException("Приложение уже инициализировано."); } Locale.StringSet.Load(); instance = this; assemblyInfo = new AssemblyInfo(); appCache = new AppCache(); userInfo = new UserInfo(); // App config appConfig = AppConfig.Load(); // DB if (appConfig.dataProvider.Trim().Length == 0 || appConfig.connStr.Trim().Length == 0 || appConfig.debugMode == 2) { CreateDbForm form = new CreateDbForm("Data", "HospitalDepartment"); form.DataProviders.Add(SqlServerProvider.name); if (form.ShowDialog() == DialogResult.OK) { connectionFactory = form.DbCreationProperties.CreateDb(); // create database UpdateScripts.UpdateDb(connectionFactory, null, "HospitalDepartment.Resources.CreationScripts.sql"); appConfig.dataProvider = connectionFactory.ProviderFactory.Name; appConfig.connStr = connectionFactory.ConnectionString; appConfig.Save(); // import tables ImportTable.ImportData(connectionFactory, PathUtils.BaseDirectory + "Import"); } else { return(false); } } else { connectionFactory = new ConnectionFactory(appConfig.dataProvider, appConfig.connStr); } // Hospital department config config = Config.Load(); ConfigUpdate.CheckUpdate(config, true); // Connection test using (GmConnection conn = App.CreateConnection()) { conn.DbConnection.Open(); } Log.LogSystem.AddLogHandlers(new SqlLog(connectionFactory)); Log.LogSystem.OnLogMessage += new EventHandler <LogMessageEventArgs>(LogHandler_OnLogMessage); Log.Info("AppStart", "Запуск программы."); // update database UpdateScripts.UpdateDb(connectionFactory, null, "HospitalDepartment.Resources.UpdateScripts.sql"); // load icon Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream(App.DepartmentConfig.iconName)) { icon = new Icon(stream); } } // Login if (App.DebugMode > 0 && System.Diagnostics.Debugger.IsAttached) // autologin { Login("1", "1"); } else { LoginForm form = new LoginForm(false); form.ShowDialog(); } if (UserId == 0 || Role == null) { return(false); } Log.Info("Пользователь вошел в систему", "UserId={0} RoleId={1}", UserId, Role.Id); // App cache using (WaitCursor wc = new WaitCursor()) { using (GmConnection conn = App.CreateConnection()) { appCache.Update(conn, this.Role.watchingGroupId); } } // Check watching if (userInfo.HasWatching) { if (userInfo.Watching.Id == 0) { WatchingForm watchingForm = new WatchingForm(this); if (watchingForm.ShowDialog() != DialogResult.OK) { return(false); } } if (userInfo.Watching.Id == 0) { return(false); } } taskManager = new TaskManager(userInfo, config, appCache); return(true); }