private bool CreateDatabase() { bool error = false; string connectString = SqlServers.BuildConnectionString(optCreationIntegratedSecurity.Checked, string.Empty, cboCreationServerName.Text, txtCreationUserName.Text, txtCreationPassword.Text); if (SqlServers.TestConnectionString(connectString) && SqlServers.HasCreatePermissions(connectString)) { try { var setup = new InstallSetup() { MasterConnectionString = connectString, NewDatabaseName = txtCreationDatabaseName.Text, }; SqlServers.CreateDatabase(setup); } catch (Exception ex) { error = true; System.Diagnostics.Debug.WriteLine(ex.ToString()); MessageBox.Show("Could not create database." + Environment.NewLine + ex.Message); } } else { error = true; MessageBox.Show("The account does not have permissions to create a database on this server.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(error); }
/// <summary> /// Performs an install of a database /// </summary> public void Install(InstallSetup setup) { if (setup.InstallStatus == InstallStatusConstants.Create) { //Connection cannot reference an existing database if (SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string references an existing database."); } //The new database name must be specified if (string.IsNullOrEmpty(setup.NewDatabaseName)) { throw new Exception("A new database name was not specified."); } //The connection string and the new database name must be the same var builder = new SqlConnectionStringBuilder(setup.ConnectionString); if (builder.InitialCatalog.ToLower() != setup.NewDatabaseName.ToLower()) { throw new Exception("A new database name does not match the specified connection string."); } SqlServers.CreateDatabase(setup); } else if (setup.InstallStatus == InstallStatusConstants.Upgrade) { //The connection string must reference an existing database if (!SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string does not reference a valid database."); } } try { UpgradeInstaller.UpgradeDatabase(setup); } catch (InvalidSQLException ex) { var sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("BEGIN ERROR SQL"); sb.AppendLine(ex.SQL); sb.AppendLine("END ERROR SQL"); sb.AppendLine(); System.Diagnostics.Trace.WriteLine(sb.ToString()); UpgradeInstaller.LogError(ex, sb.ToString()); throw; } catch (Exception ex) { throw; } }
private bool CreateDatabase() { var error = false; var connectString = SqlServers.BuildConnectionString(optCreationIntegratedSecurity.Checked, string.Empty, cboCreationServerName.Text, txtCreationUserName.Text, txtCreationPassword.Text); if (SqlServers.TestConnectionString(connectString) && SqlServers.HasCreatePermissions(connectString)) { if (!string.IsNullOrWhiteSpace(txtDiskPath.Text) && !Directory.Exists(txtDiskPath.Text)) { error = true; MessageBox.Show("The specified disk path does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(error); } try { var setup = new InstallSetup() { MasterConnectionString = connectString, NewDatabaseName = txtCreationDatabaseName.Text, DiskPath = txtDiskPath.Text.Trim(), }; SqlServers.CreateDatabase(setup); } catch (Exception ex) { error = true; System.Diagnostics.Debug.WriteLine(ex.ToString()); MessageBox.Show("Could not create database." + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { error = true; MessageBox.Show("The account does not have permissions to create a database on this server.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(error); }
/// <summary> /// Performs an install of a database /// </summary> public void Install(InstallSetup setup) { if (setup.InstallStatus == InstallStatusConstants.Create) { //Conection cannot reference an existing database if (SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string references an existing database."); } //The new database name must be specified if (string.IsNullOrEmpty(setup.NewDatabaseName)) { throw new Exception("A new database name was not specified."); } //The connection string and the new database name must be the same var builder = new System.Data.SqlClient.SqlConnectionStringBuilder(setup.ConnectionString); if (builder.InitialCatalog.ToLower() != setup.NewDatabaseName.ToLower()) { throw new Exception("A new database name does not match the specified connection string."); } SqlServers.CreateDatabase(setup); } else if (setup.InstallStatus == InstallStatusConstants.Upgrade) { //The connection string must reference an existing database if (!SqlServers.TestConnectionString(setup.ConnectionString)) { throw new Exception("The connection string does not reference a valid database."); } } UpgradeInstaller.UpgradeDatabase(setup); }