public void Install(string masterConnectionString, string newDatabaseName, string newDatabaseConnectionString) { SqlServers.CreateDatabase(masterConnectionString, newDatabaseName); DateTime startTime = DateTime.Now; while (DateTime.Now.Subtract(startTime).TotalSeconds < 9) { Application.DoEvents(); } try { UpgradeInstaller.UpgradeDatabase(newDatabaseConnectionString, true); } catch (Exception ex) { System.Threading.Thread.Sleep(8000); try { UpgradeInstaller.UpgradeDatabase(newDatabaseConnectionString, true); } catch { throw; } } }
private void Initialize() { string dbVersion = SqlServers.GetDatabaseExtendedProperty(_connectionString, "dbVersion"); if (dbVersion != string.Empty) { string[] versionNumbers = dbVersion.Split('.'); _previousVersion = new Version(); _previousVersion.Major = int.Parse(versionNumbers[0]); _previousVersion.Minor = int.Parse(versionNumbers[1]); _previousVersion.Revision = int.Parse(versionNumbers[2]); _previousVersion.Build = int.Parse(versionNumbers[3]); } Assembly assem = Assembly.GetExecutingAssembly(); string[] resourceNames = assem.GetManifestResourceNames(); foreach (string resourceName in resourceNames) { EmbeddedResourceName ern = new EmbeddedResourceName(resourceName); _resourceNames.Add(ern); } }
private void RunUpgrade() { _connection = new System.Data.SqlClient.SqlConnection(_connectionString); _connection.Open(); _transaction = _connection.BeginTransaction(); try { this.UpgradeSchemaAndStaticData(); this.ReinstallStoredProcedures(); _transaction.Commit(); SqlServers.UpdateDatabaseExtendedProperty(_connectionString, "dbVersion", _upgradeToVersion.ToString(".")); SqlServers.UpdateDatabaseExtendedProperty(_connectionString, "LastUpdate", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)); } catch (Exception ex) { _transaction.Rollback(); throw; } finally { _connection.Close(); } }
private void ReinstallStoredProcedures() { try { SortedDictionary <string, EmbeddedResourceName> storedProcedures = this.GetResourceNameUnderLocation(".Stored_Procedures"); foreach (EmbeddedResourceName ern in storedProcedures.Values) { try { SqlServers.RunEmbeddedFile(_connection, _transaction, ern.FullName); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("Sp Fail: " + ern.FullName); throw; } } } catch (Exception ex) { throw; } }
private void UpgradeSchemaAndStaticData() { const string CREATE_SCHEMA_FILE = ".Create_Scripts.Generated.CreateSchema.sql"; const string STATIC_DATA_FILE = ".Create_Scripts.Generated.CreateData.sql"; const string UPGRADE_FOLDER = ".Upgrade_Scripts"; const string UPGRADE_GENERATED_FOLDER = ".Upgrade_Scripts.Generated."; const string CREATE_FOLDER = ".Cre ate_Scripts"; try { SortedDictionary <string, EmbeddedResourceName> upgradeSchemaScripts = this.GetResourceNameUnderLocation(UPGRADE_GENERATED_FOLDER); SortedDictionary <string, EmbeddedResourceName> sortByVersionScripts = new SortedDictionary <string, EmbeddedResourceName>(upgradeSchemaScripts, new UpgradeFileNameComparer()); //Run the generated upgrades if (!_previousVersion.Equals(_def_Version)) { foreach (EmbeddedResourceName ern in sortByVersionScripts.Values) { if (new Version(ern.FileName).CompareTo(_previousVersion) > 0) { SqlServers.RunEmbeddedFile(_connection, _transaction, ern.FullName); } } } //Run the create schema SortedDictionary <string, EmbeddedResourceName> createSchemaScripts = this.GetResourceNameUnderLocation(CREATE_SCHEMA_FILE); foreach (EmbeddedResourceName ern in createSchemaScripts.Values) { SqlServers.RunEmbeddedFile(_connection, _transaction, ern.FullName); } //Run the static data createSchemaScripts = this.GetResourceNameUnderLocation(STATIC_DATA_FILE); foreach (EmbeddedResourceName ern in createSchemaScripts.Values) { SqlServers.RunEmbeddedFile(_connection, _transaction, ern.FullName); } //Run all other scripts SortedDictionary <string, EmbeddedResourceName> createDataScripts = this.GetResourceNameUnderLocation(CREATE_FOLDER); foreach (EmbeddedResourceName ern in createDataScripts.Values) { //Make sure it is not the two defined scripts if ((ern.AsmLocation != CREATE_SCHEMA_FILE) && (ern.AsmLocation != STATIC_DATA_FILE)) { SqlServers.RunEmbeddedFile(_connection, _transaction, ern.FullName); } } //Run the user-defined upgrades List <string> excludePathList = new List <string>(); excludePathList.Add(UPGRADE_GENERATED_FOLDER); upgradeSchemaScripts = this.GetResourceNameUnderLocation(UPGRADE_FOLDER, excludePathList); sortByVersionScripts = new SortedDictionary <string, EmbeddedResourceName>(upgradeSchemaScripts, new UpgradeFileNameComparer()); if (_previousVersion != null) { foreach (EmbeddedResourceName ern in sortByVersionScripts.Values) { if (new Version(ern.FileName).CompareTo(_previousVersion) > 0) { SqlServers.RunEmbeddedFile(_connection, _transaction, ern.FullName); } } } } catch (Exception ex) { throw; } }