예제 #1
0
        // Запуск выбранного sql-скрипта
        private void runSqlScriptButton_Click(object sender, EventArgs e)
        {
            SqlConnection myConn = new SqlConnection("Data Source =.\\SQLEXPRESS; Integrated Security = True");
            string        script = File.ReadAllText(sqlScriptFileTextBox.Text);

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            IEnumerable <string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);

            try
            {
                myConn.Open();
                foreach (string commandString in commandStrings)
                {
                    if (commandString.Trim() != "")
                    {
                        using (var command = new SqlCommand(commandString, myConn))
                        {
                            command.ExecuteNonQuery();
                        }
                    }
                }
                SystemSounds.Beep.Play();
                MessageBox.Show("Успешно выполено");

                SqlConnectionStringBuilder sConnB = new SqlConnectionStringBuilder()
                {
                    DataSource         = Properties.Settings.Default.userServerName,
                    InitialCatalog     = Properties.Settings.Default.userServerDatabase,
                    IntegratedSecurity = true
                };
                HistoryRecordsController.ChangeSqlConnection(sConnB.ConnectionString);
            }
            catch (Exception ex)
            {
                HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка при запуске sql-скрипта в runSqlScriptButton_Click.");
                MessageBox.Show("Ошибка при запуске sql-скрипта.\nВызвано исключение: " + ex.Message);
            }
            finally
            {
                myConn.Close();
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
        }
예제 #2
0
        // Сохранение настроек программы
        private void saveSettingsButton_Click(object sender, EventArgs e)
        {
            try
            {
                System.Windows.Forms.Cursor.Current                 = System.Windows.Forms.Cursors.WaitCursor;
                Properties.Settings.Default.userServerName          = userServerNameTextBox.Text;
                Properties.Settings.Default.userServerDatabase      = userServerDatabaseTextBox.Text;
                Properties.Settings.Default.userServer2Name         = userServer2NameTextBox.Text;
                Properties.Settings.Default.userServer2Database     = userServer2DatabaseTextBox.Text;
                Properties.Settings.Default.adminServerName         = adminServerNameTextBox.Text;
                Properties.Settings.Default.adminServerDatabase     = adminServerDatabaseTextBox.Text;
                Properties.Settings.Default.backupsFolderPath       = backupsFolderPathTextBox.Text;
                Properties.Settings.Default.restoreFilePath         = restoreFilePathTextBox.Text;
                Properties.Settings.Default.exceptionsLogFolderPath = exceptionsLogFolderPathTextBox.Text;
                Properties.Settings.Default.Save();

                SqlConnectionStringBuilder sConnB = new SqlConnectionStringBuilder()
                {
                    DataSource         = Properties.Settings.Default.userServerName,
                    InitialCatalog     = Properties.Settings.Default.userServerDatabase,
                    IntegratedSecurity = true
                };
                HistoryRecordsController.ChangeSqlConnection(sConnB.ConnectionString);

                SystemSounds.Exclamation.Play();
                MessageBox.Show("Настройки успешно сохранены!");
            }
            catch (Exception ex)
            {
                HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка при сохранении настроек программы в saveSettingsButton_Click.");
                MessageBox.Show("Ошибка при сохранении настроек программы.\nВызвано исключение: " + ex.Message);
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
        }
예제 #3
0
 // Проверка существования базы данных. Если бд не существует, открывается окно
 // выбора sql-скрипта для создания новой базы данных.
 private void LoginForm_Load(object sender, EventArgs e)
 {
     try
     {
         SqlConnectionStringBuilder sConnB = new SqlConnectionStringBuilder()
         {
             DataSource         = Properties.Settings.Default.userServerName,
             InitialCatalog     = Properties.Settings.Default.userServerDatabase,
             IntegratedSecurity = true
         };
         sqlConnection = new SqlConnection(sConnB.ConnectionString);
         sqlConnection.Open();
         HistoryRecordsController.ChangeSqlConnection(sConnB.ConnectionString);
     }
     catch (SqlException sqlEx)
     {
         SystemSounds.Exclamation.Play();
         HistoryRecordsController.WriteExceptionToLogFile(sqlEx, "Ошибка подключения к базе данных при запуске программы.");
         MessageBox.Show("Не удалось подключиться к базе данных. Ошибка:\n" + sqlEx.Message, "Ошибка");
         OpenFileDialog dlg      = new OpenFileDialog();
         string         filePath = "";
         dlg.Filter      = "Sql scripts(*.sql)|*.sql|All Files(*.*)|*.*";
         dlg.FilterIndex = 0;
         if (dlg.ShowDialog() == DialogResult.OK)
         {
             filePath = dlg.FileName;
         }
         SqlConnectionStringBuilder sConnB2 = new SqlConnectionStringBuilder()
         {
             DataSource         = Properties.Settings.Default.userServerName,
             IntegratedSecurity = true
         };
         SqlConnection        myConn         = new SqlConnection(sConnB2.ConnectionString);
         string               script         = File.ReadAllText(filePath);
         IEnumerable <string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
         try
         {
             myConn.Open();
             foreach (string commandString in commandStrings)
             {
                 if (commandString.Trim() != "")
                 {
                     using (var command = new SqlCommand(commandString, myConn))
                     {
                         command.ExecuteNonQuery();
                     }
                 }
             }
             SystemSounds.Beep.Play();
             MessageBox.Show("База данных создана");
         }
         catch (SqlException ex)
         {
             SystemSounds.Exclamation.Play();
             HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка создания базы данных через sql-скрипт.");
             MessageBox.Show("Ошибка создания базы данных:\n" + sqlEx.Message);
         }
         finally
         {
             myConn.Close();
         }
     }
     finally
     {
         sqlConnection.Close();
     }
 }