private static void InitializeSqlServerDb() { using (var conn = DBUtils.CreateSqlServerConnection("", Settings.Default.sqlServerHost, Settings.Default.sqlServerUsername, EncryptionUtils.Unprotect(Settings.Default.sqlServerPassword), useWindowsAuthentication: Settings.Default.sqlServerUseWindowsAuthentication, noDB: true)) { conn.Open(); using (var cmd = new SqlCommand("", conn)) { cmd.CommandText = @"SELECT database_id FROM sys.databases WHERE Name = 'qdmsQuartz'"; object id = cmd.ExecuteScalar(); if (id == null) { //db does not exist, create it var commands = new List <string>(); commands.Add("CREATE DATABASE qdmsQuartz"); commands.Add("USE qdmsQuartz"); Regex regex = new Regex(@"\r{0,1}\nGO\r{0,1}\n"); commands.AddRange(regex.Split(Resources.QuartzSqlServerDbInit)); //needed because all the 'GO' things in the script; foreach (string command in commands) { cmd.CommandText = command; cmd.ExecuteNonQuery(); } } } } }
private void SqlServerOKBtn_Click(object sender, RoutedEventArgs e) { bool windowsAuth = WindowsAuthenticationRadioBtn.IsChecked ?? true; SqlConnection connection = DBUtils.CreateSqlServerConnection( server: SqlServerHostTextBox.Text, username: SqlServerUsernameTextBox.Text, password: SqlServerPasswordTextBox.Password, useWindowsAuthentication: windowsAuth, noDB: true); try { connection.Open(); } catch (Exception ex) { MessageBox.Show("Connection failed with error: " + ex.Message); return; } if (WindowsAuthenticationRadioBtn.IsChecked != null) { Properties.Settings.Default.sqlServerUseWindowsAuthentication = WindowsAuthenticationRadioBtn.IsChecked.Value; } Properties.Settings.Default.sqlServerHost = SqlServerHostTextBox.Text; Properties.Settings.Default.sqlServerUsername = SqlServerUsernameTextBox.Text; Properties.Settings.Default.sqlServerPassword = EncryptionUtils.Protect(SqlServerPasswordTextBox.Password); Properties.Settings.Default.databaseType = "SqlServer"; Properties.Settings.Default.Save(); Close(); }
public static bool TestConnection() { if (Settings.Default.databaseType == "MySql") { //try to establish a database connection. If not possible, prompt the user to enter details var connection = DBUtils.CreateMySqlConnection(noDB: true); try { connection.Open(); } catch (Exception) { return(false); } connection.Close(); } else //SQL Server { //try to establish a database connection. If not possible, prompt the user to enter details var connection = DBUtils.CreateSqlServerConnection(noDB: true, useWindowsAuthentication: Settings.Default.sqlServerUseWindowsAuthentication); try { connection.Open(); } catch (Exception) { return(false); } connection.Close(); } return(true); }
private void SqlServerTestConnectionBtn_Click(object sender, RoutedEventArgs e) { bool windowsAuth = WindowsAuthenticationRadioBtn.IsChecked ?? true; SqlConnection connection = DBUtils.CreateSqlServerConnection( server: SqlServerHostTextBox.Text, username: SqlServerUsernameTextBox.Text, password: SqlServerPasswordTextBox.Password, useWindowsAuthentication: windowsAuth, noDB: true); try { connection.Open(); } catch (Exception ex) { MessageBox.Show("Connection failed with error: " + ex.Message); return; } MessageBox.Show("Connection succeeded."); connection.Close(); }