void Worker_DoWork(object sender, DoWorkEventArgs e)
 {
     foreach (var server in SQLFunctions.GetServers())
     {
         if (!HostCoBox.Items.Contains(server))
         {
             if (this.HostCoBox.InvokeRequired)
             {
                 HostCoBox.BeginInvoke((MethodInvoker) delegate()
                 {
                     HostCoBox.Items.Add(server);
                 });
             }
             else
             {
                 HostCoBox.Items.Add(server);
             }
         }
     }
 }
 private void DBCoBox_Enter(object sender, EventArgs e)
 {
     using (var con = SQLFunctions.GetConnection(HostCoBox.Text, DBCoBox.Text, AuthCoBox.SelectedIndex == 1, AuthCoBox.SelectedIndex == 0 ? UsernameTxt.Text : string.Empty, AuthCoBox.SelectedIndex == 0 ? PassTxt.Text : string.Empty))
     {
         if (con != null)
         {
             //DBCoBox.Enabled = true;
             foreach (var db in SQLFunctions.GetDatabases(con))
             {
                 if (!DBCoBox.Items.Contains(db))
                 {
                     DBCoBox.Items.Add(db);
                 }
             }
         }
         else
         {
             //DBCoBox.Enabled = false;
         }
     }
 }
        private void MyBackupTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (this.TimerInUse)
            {
                return;
            }
            this.TimerInUse = true;

            for (int i = 0; i < MyBackup.MyBackups.Count; i++)
            {
                var backup = MyBackup.MyBackups[i];

                if (backup.IsEnabled)
                {
                    var Remaining = GetRemainingTime(backup, out long At);
                    if (Remaining.TotalSeconds <= 0)
                    {
                        string FileName = backup.BackupName +
                                          "_" +
                                          backup.Database +
                                          "_" +
                                          DateTime.Now.ToString() + ".bak";

                        Vars.GiveEveryoneAccessControl(backup.BackupPath);

                        FileName = Vars.RemoveInvalidFileNameChars(FileName);
                        var path = Path.Combine(backup.BackupPath, FileName);

                        var ConnectionString = SQLFunctions.GetConnectionString(backup.Host, backup.Database, backup.Authentication, backup.Username, backup.Password);

                        var backupResult = SQLFunctions.BackupNow(path, ConnectionString);
                        var backupState  = new BackupState()
                        {
                            backup                = backup,
                            BackupDateTime        = DateTime.Now.Ticks,
                            DefaultBackupDateTime = At,
                            FullPath              = path,
                            Warning               = backupResult != null,
                        };

                        if (backupResult != null)
                        {
                            backupState.WarningMessage = backupResult.Message;
                            backupState.FullWarning    = backupResult.ToString();
                        }
                        MyBackup.SaveBackupState(backupState);
                        backup.LastBackup = DateTime.Now.Ticks;
                        MyBackup.SaveBackup(backup);
                    }
                }

                if (this.MyTreeView.InvokeRequired)
                {
                    this.MyTreeView.BeginInvoke((MethodInvoker) delegate()
                    {
                        this.RefreshMyTreeView(backup);
                    });
                }
                else
                {
                    this.RefreshMyTreeView(backup);
                }
            }

            this.TimerInUse = false;
        }
 private void TestConnectionBtn_Click(object sender, EventArgs e)
 {
     SQLFunctions.TestConnection(true, HostCoBox.Text, DBCoBox.Text, AuthCoBox.SelectedIndex == 1, AuthCoBox.SelectedIndex == 0 ? UsernameTxt.Text : string.Empty, AuthCoBox.SelectedIndex == 0 ? PassTxt.Text : string.Empty, out _, out _);
 }
        private void SaveBackup_Click(object sender, EventArgs e)
        {
            var BackupName = BackupNameTxt.Text.Trim();

            #region Save Validation
            int index = ((MyBackup)MyParentPanel.Tag).MyIndex;
            if (BackupName.Length == 0)
            {
                MessageBox.Show("Please write a valid Backup Name!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                BackupNameTxt.Focus();
                return;
            }
            else if (MyBackup.MyBackups.Exists(x => x.BackupName.ToLower() == BackupName.ToLower() && x.MyIndex != index))
            {
                MessageBox.Show("Backup Name is already exists!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                BackupNameTxt.Focus();
                return;
            }
            else if (!SQLFunctions.TestConnection(false, HostCoBox.Text, DBCoBox.Text, AuthCoBox.SelectedIndex == 1, AuthCoBox.SelectedIndex == 0 ? UsernameTxt.Text : string.Empty, AuthCoBox.SelectedIndex == 0 ? PassTxt.Text : string.Empty, out _, out string ExcMessage))
            {
                MessageBox.Show(ExcMessage, "SQL ConnectionString Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                HostCoBox.Focus();
                return;
            }
            else if (BackupPathTxt.Text.Length == 0 || !Directory.Exists(BackupPathTxt.Text))
            {
                MessageBox.Show("Please select a valid Backup Path!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                BackupPathTxt.Focus();
                return;
            }
            else if (!RepeatRadio.Checked && !SpecificRadio.Checked)
            {
                MessageBox.Show("Please select a valid Backup Schedule!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                RepeatRadio.Focus();
                return;
            }
            else if (RepeatRadio.Checked)
            {
                if (!SatBox.Checked && !SunBox.Checked && !MonBox.Checked && !TueBox.Checked && !WedBox.Checked && !ThuBox.Checked && !FriBox.Checked)
                {
                    MessageBox.Show("Please select one day schedule at least!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    SatBox.Focus();
                    return;
                }
            }
            else if (SpecificRadio.Checked)
            {
                if (!MinutesRadio.Checked && !HoursRadio.Checked && !DaysRadio.Checked && !WeeksRadio.Checked && !MonthsRadio.Checked)//YearsRadio
                {
                    MessageBox.Show("Please select a schedule type!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    MinutesRadio.Focus();
                    return;
                }
            }
            #endregion

            var backup = (MyBackup)MyParentPanel.Tag;

            //Backup Options
            backup.IsEnabled  = IsEnabledBox.Checked;
            backup.BackupName = BackupName;

            //SQL Connection
            string host;
            if (HostCoBox.SelectedItem != null)
            {
                host = HostCoBox.SelectedItem.ToString();
            }
            else
            {
                host = HostCoBox.SelectedText;
            }
            backup.Host = host;

            string db;
            if (DBCoBox.SelectedItem != null)
            {
                db = DBCoBox.SelectedItem.ToString();
            }
            else
            {
                db = DBCoBox.SelectedText;
            }
            backup.Database = db;

            backup.Authentication = AuthCoBox.SelectedIndex == 1;
            backup.Username       = UsernameTxt.Text;
            backup.Password       = PassTxt.Text;
            backup.SavePassword   = SaveCkBox.Checked;

            //Backup Path
            backup.BackupPath = BackupPathTxt.Text;

            //Backup Repeat Datetime
            backup.Repeat = RepeatRadio.Checked;
            backup.Time   = new TimeSpan(TimePicker.Value.Hour, TimePicker.Value.Minute, 0).Ticks;//new TimeSpan(TimePicker.Value.Hour == 0 ? 1 : 0, TimePicker.Value.Hour, TimePicker.Value.Minute, 0).Ticks;
            backup.Sat    = SatBox.Checked;
            backup.Sun    = SunBox.Checked;
            backup.Mon    = MonBox.Checked;
            backup.Tue    = TueBox.Checked;
            backup.Wed    = WedBox.Checked;
            backup.Thu    = ThuBox.Checked;
            backup.Fri    = FriBox.Checked;

            //Backup Specific Datetime
            backup.Specific = SpecificRadio.Checked;
            backup.Every    = SpecificNum.Value;
            if (MinutesRadio.Checked)
            {
                backup.SpecificType = SpecificTypes.Minutes;
            }
            else if (HoursRadio.Checked)
            {
                backup.SpecificType = SpecificTypes.Hours;
            }
            else if (DaysRadio.Checked)
            {
                backup.SpecificType = SpecificTypes.Days;
            }
            else if (WeeksRadio.Checked)
            {
                backup.SpecificType = SpecificTypes.Weeks;
            }
            else if (MonthsRadio.Checked)
            {
                backup.SpecificType = SpecificTypes.Months;
            }
            //Years

            if (index == 0 || backup.LastBackup < 1)
            {
                backup.LastBackup = DateTime.Now.Ticks;
            }

            MyBackup.SaveBackup(backup);
            RefreshMyTreeView(backup);
            MessageBox.Show("Backup settings have been saved successfully.", "Backup settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }