예제 #1
0
        public void BackupFunc()
        {
            if (InfoTab.AppState != AppStates.Awaiting)
            {
                InfoTab = new InfoTabViewItem(AppStates.IsBusy);
                OnPropertyChanged(nameof(InfoTab));

                try
                {
                    var fileName = $"{ConnectionStringModel.DbNameText} Backup {DateTime.Now.ToShortDateString()}.bak";

                    using (SqlConnection connection = new SqlConnection(ConnectionStringModel.ConnectionString))
                    {
                        var commandText = $@"BACKUP DATABASE {ConnectionStringModel.DbNameText} TO  DISK = N'{ConnectionStringModel.PathText}\{fileName}' 
                                          WITH NOFORMAT, INIT,  NAME = N'{ConnectionStringModel.DbNameText}-Полная База данных Резервное копирование', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";

                        var command = new SqlCommand(commandText, connection);
                        command.CommandText    = commandText;
                        command.CommandTimeout = 3000;
                        connection.Open();
                        command.ExecuteNonQuery();
                    }

                    if (Settings.Default.Paths != null)
                    {
                        if (!Settings.Default.Paths.Contains(ConnectionStringModel.PathText) && !string.IsNullOrEmpty(ConnectionStringModel.PathText))
                        {
                            Settings.Default.Paths.Add(ConnectionStringModel.PathText);
                            Settings.Default.Save();
                        }
                    }
                    else
                    {
                        Settings.Default.Paths = new System.Collections.Specialized.StringCollection();
                        Settings.Default.Paths.Add(ConnectionStringModel.PathText);
                        Settings.Default.Save();
                    }

                    InfoTab = new InfoTabViewItem(AppStates.SuccessfullyBackuped);
                    OnPropertyChanged(nameof(InfoTab));
                }
                catch (Exception ex)
                {
                    Clipboard.SetText(ex.Message);
                    InfoTab = new InfoTabViewItem(AppStates.BackupError);
                    OnPropertyChanged(nameof(InfoTab));
                }
            }
            else
            {
                MessageBox.Show("Проверьте соединение перед выполнением операции!");
            }
        }
예제 #2
0
        public void RestoreFunc()
        {
            if (InfoTab.AppState != AppStates.Awaiting)
            {
                InfoTab = new InfoTabViewItem(AppStates.IsBusy);
                OnPropertyChanged(nameof(InfoTab));

                try
                {
                    using (SqlConnection connection = new SqlConnection(ConnectionStringModel.ConnectionString))
                    {
                        var commandText = $@"RESTORE DATABASE {ConnectionStringModel.DbNameText} FROM DISK = '{ConnectionStringModel.PathText}'";

                        var command = new SqlCommand(commandText, connection);
                        command.CommandText    = commandText;
                        command.CommandTimeout = 3000;
                        connection.Open();
                        command.ExecuteNonQuery();
                    }

                    if (Settings.Default.Paths != null)
                    {
                        if (!Settings.Default.Paths.Contains(ConnectionStringModel.PathText) && !string.IsNullOrEmpty(ConnectionStringModel.PathText))
                        {
                            Settings.Default.Paths.Add(ConnectionStringModel.PathText);
                            Settings.Default.Save();
                        }
                    }
                    else
                    {
                        Settings.Default.Paths = new System.Collections.Specialized.StringCollection();
                        Settings.Default.Paths.Add(ConnectionStringModel.PathText);
                        Settings.Default.Save();
                    }

                    InfoTab = new InfoTabViewItem(AppStates.SuccessfullyRestored);
                    OnPropertyChanged(nameof(InfoTab));
                }
                catch (Exception ex)
                {
                    Clipboard.SetText(ex.Message);
                    InfoTab = new InfoTabViewItem(AppStates.RestoreError);
                    OnPropertyChanged(nameof(InfoTab));
                }
            }
            else
            {
                MessageBox.Show("Проверьте соединение перед выполнением операции!");
            }
        }
예제 #3
0
        public MainModel()
        {
            CheckConnectionCommand = new DelegateCommand(x => CheckConnection());
            BackupCommand          = new DelegateCommand(x => Backup());
            PathChoiceCommand      = new DelegateCommand(x => PathChoice());
            RestoreCommand         = new DelegateCommand(x => Restore());


            InfoTab = new InfoTabViewItem(AppStates.Awaiting);

            if (ConnectionStringModel.SelectedAuthenticationKind.Value == null)
            {
                ConnectionStringModel.SelectedAuthenticationKind = ConnectionStringModel.AuthenticationKinds().FirstOrDefault(x => x.Key == AuthenticationKind.Windows);
            }
        }
예제 #4
0
        public void CheckConnectionFunc()
        {
            InfoTab = new InfoTabViewItem(AppStates.IsBusy);
            OnPropertyChanged(nameof(InfoTab));

            if (ConnectionStringModel.SelectedAuthenticationKind.Key == AuthenticationKind.Windows)
            {
                ConnectionStringModel.ConnectionString = $"Server={ConnectionStringModel.ServerNameText};Database=master;Trusted_Connection=True;";

                var isSuccessfull = ConnectionStringModel.IsSQLConnected(ConnectionStringModel.ConnectionString);

                if (isSuccessfull)
                {
                    SaveSettings();
                    InfoTab = new InfoTabViewItem(AppStates.Connected);
                }
                else
                {
                    InfoTab = new InfoTabViewItem(AppStates.ConnectionError);
                }
            }
            else
            {
                ConnectionStringModel.ConnectionString = $"Server={ConnectionStringModel.ServerNameText};Database={ConnectionStringModel.DbNameText};User Id={ConnectionStringModel.UserNameText};" +
                                                         $"Password={ConnectionStringModel.PasswordText};";

                var isSuccessfull = ConnectionStringModel.IsSQLConnected(ConnectionStringModel.ConnectionString);

                if (isSuccessfull)
                {
                    SaveSettings();
                    InfoTab = new InfoTabViewItem(AppStates.Connected);
                }
                else
                {
                    InfoTab = new InfoTabViewItem(AppStates.ConnectionError);
                }
            }
            OnPropertyChanged(nameof(InfoTab));
        }