private void btnSaoLuuAd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Program.Db.Trim() == "" || tenDevice == "")
            {
                return;
            }

            String strBackup = "BACKUP DATABASE " + Program.Db + " TO " + tenDevice;

            if (ckiNit.Checked == true)
            {
                if (MessageBox.Show("Bạn có muốn xóa các sao lưu cũ ?", "Xác nhận", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    strBackup = strBackup + " WITH INIT";
                }
            }

            try
            {
                Program.myreader = Program.ExecSqlDataReader(strBackup);

                if (Program.myreader != null)
                {
                    int i;
                    this.PrgLoad.Visible = true;
                    this.PrgLoad.Minimum = 0;
                    this.PrgLoad.Maximum = 100;
                    this.PrgLoad.Step    = 10;
                    for (i = this.PrgLoad.Minimum; i <= this.PrgLoad.Maximum; i++)
                    {
                        PrgLoad.Value = i;
                        PrgLoad.PerformStep();
                        Thread.Sleep(10);
                    }
                    PrgLoad.Visible = false;
                    MessageBox.Show(" Tạo Backup thành công!", "", MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show(" Tạo Backup thất bại!", "", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex, "!", MessageBoxButtons.OK);
                return;
            }
            Program.conn.Close();
            LoadBanSaoLuu();
        }
        private void taoDevice()
        {
            tenDevice         = "DEVICE_" + Program.Db;
            strFullPathDevice = Program.strDefaultPath + tenDevice + ".BAK";

            String query = "USE master\n EXEC sp_addumpdevice '" + Program.device_type + "', '" + tenDevice + "','" + strFullPathDevice + "'";

            try
            {
                Program.myreader = Program.ExecSqlDataReader(query);
                if (Program.myreader != null)
                {
                    int i;
                    this.PrgLoad.Visible = true;
                    this.PrgLoad.Minimum = 0;
                    this.PrgLoad.Maximum = 100;
                    this.PrgLoad.Step    = 10;

                    for (i = this.PrgLoad.Minimum; i <= this.PrgLoad.Maximum; i++)
                    {
                        PrgLoad.Value = i;

                        PrgLoad.PerformStep();
                        Thread.Sleep(10);
                    }

                    PrgLoad.Visible = false;
                    MessageBox.Show(" Tạo Device thành công!", "", MessageBoxButtons.OK);
                    this.backup_devicesTableAdapter.Fill(this.DS.backup_devices);
                    btnDevice.Enabled   = false;
                    btnSaoLuuAd.Enabled = true;
                    txtTenDevice.Text   = tenDevice;
                }
                else
                {
                    MessageBox.Show(" Tạo Device thất bại!", "", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex, "!", MessageBoxButtons.OK);
                return;
            }

            Program.conn.Close();
        }
        private void RestoreDiaglog(String dtStopAt)
        {
            DialogResult rsdiaglog = MessageBox.Show("Thời gian phục hồi: " + dtStopAt, Program.Db + " - Restore dialog", MessageBoxButtons.OKCancel);

            if (rsdiaglog == DialogResult.OK)
            {
                String strFullPathBackLog = Program.strDefaultPath + Program.Db + ".TRN";
                strRestore += "BACKUP LOG " + Program.Db + " TO DISK='" + strFullPathBackLog + "' WITH INIT\n "
                              + "RESTORE DATABASE " + Program.Db + " FROM DISK='" + strFullPathDevice + "' WITH NORECOVERY , REPLACE\n "
                              + "RESTORE DATABASE " + Program.Db + " FROM DISK='" + strFullPathBackLog + "' WITH STOPAT='" + dtStopAt
                              + "'\n ALTER DATABASE " + Program.Db + " SET MULTI_USER";
                int checkErr = Program.ExecSqlNonQuery(strRestore, Program.connstr, "lỗi phục hồi cơ sở dữ liệu");
                if (checkErr == 0)
                {
                    int i;
                    this.PrgLoad.Visible = true;
                    this.PrgLoad.Minimum = 0;
                    this.PrgLoad.Maximum = 100;
                    this.PrgLoad.Step    = 20;
                    for (i = this.PrgLoad.Minimum; i <= this.PrgLoad.Maximum; i++)
                    {
                        this.PrgLoad.Value = i;
                        PrgLoad.PerformStep();
                        Thread.Sleep(10);
                    }
                    PrgLoad.Visible = false;
                    MessageBox.Show(" Phục hồi thời gian lúc " + dtStopAt + " Thành công!", "", MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show(" Phục hồi thất bại ", "", MessageBoxButtons.OK);
                }
            }

            else
            {
                return;
            }
        }
        private void btnPhucHoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int err;

            if (this.bdsBackup.Count == 0)
            {
                MessageBox.Show("Chưa có bản sao lưu nào phụ hồi", "!", MessageBoxButtons.OK);
                return;
            }
            if (soLuongBanSaoLuu == 0)
            {
                MessageBox.Show("Vui lòng chọn bản sao lưu phục hồi ", "!", MessageBoxButtons.OK);
                return;
            }
            if (Program.conn != null && Program.conn.State == ConnectionState.Open)
            {
                Program.conn.Close(); // đóng kết nối
            }
            if (txtTenDB.Text.Trim() == "" || tenDevice == "")
            {
                return;
            }
            strRestore = "ALTER DATABASE " + txtTenDB.Text + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE " + "USE tempdb ";
            if (btnThamsotime.Checked == false)
            {
                strRestore += "RESTORE DATABASE " + txtTenDB.Text + " FROM " + tenDevice + " WITH FILE= " + soLuongBanSaoLuu + ",REPLACE " + "ALTER DATABASE " + Program.Db + " SET MULTI_USER";
                //   MessageBox.Show(strRestore, "", MessageBoxButtons.OK);
                err = Program.ExecSqlNonQuery(strRestore, Program.connstr, "lỗi phục hồi cơ sở dữ liệu");
                if (err == 0)
                {
                    int i;
                    this.PrgLoad.Visible = true;
                    this.PrgLoad.Minimum = 0;
                    this.PrgLoad.Maximum = 100;
                    this.PrgLoad.Step    = 20;
                    for (i = this.PrgLoad.Minimum; i <= this.PrgLoad.Maximum; i++)
                    {
                        this.PrgLoad.Value = i;

                        PrgLoad.PerformStep();
                        Thread.Sleep(10);
                    }
                    PrgLoad.Visible = false;
                    MessageBox.Show(" Phục hồi thành công!", "", MessageBoxButtons.OK);
                }
            }
            else
            {
                DateTime ngaygioBK = (DateTime)((DataRowView)bdsBackup[bdsBackup.Position])["backup_start_date"];
                DateTime dateStop  = dtpDate.Value.Date + dtptTimeStop.Value.TimeOfDay;
                String   dt        = dtpDate.Value.ToString("yyyy-MM-dd") + " " + dtptTimeStop.Value.ToString("HH:mm:ss");
                if ((dtpDate.Value.Date < ngaygioBK.Date) || (dtpDate.Value.Date == ngaygioBK.Date && dtptTimeStop.Value.Ticks < ngaygioBK.TimeOfDay.Ticks))
                {
                    MessageBox.Show(" Thời điểm muốn phục hồi phải sau thời điểm sao lưu đã chọn", "", MessageBoxButtons.OK);
                    return;
                }
                if (DateTime.Now < dateStop)
                {
                    MessageBox.Show(" Thời điểm muốn phục hồi phải trước thời điểm hiện tại", "", MessageBoxButtons.OK);
                    return;
                }
                else
                {
                    if (Directory.Exists(Program.strDefaultPath) == true)
                    {
                        RestoreDiaglog(dt);
                    }
                    else
                    {
                        OpenFileDialog folderBrowser = new OpenFileDialog();
                        folderBrowser.ValidateNames   = false;
                        folderBrowser.CheckFileExists = false;
                        folderBrowser.CheckPathExists = true;
                        folderBrowser.FileName        = "Folder Selection.";
                        if (folderBrowser.ShowDialog() == DialogResult.OK)
                        {
                            string folderPath = Path.GetDirectoryName(folderBrowser.FileName);
                            Program.strDefaultPathNew = folderPath + "\\";
                            String strFullPathBackLog = Program.strDefaultPathNew + Program.Db + ".TRN";
                            String pathBack           = Program.strDefaultPathNew + tenDevice + ".bak";
                            strRestore += "BACKUP LOG " + Program.Db + " TO DISK='" + strFullPathBackLog + "' WITH INIT\n "
                                          + "RESTORE DATABASE " + Program.Db + " FROM DISK='" + pathBack + "' WITH NORECOVERY , REPLACE\n "
                                          + "RESTORE DATABASE " + Program.Db + " FROM DISK='" + strFullPathBackLog + "' WITH STOPAT='" + dt
                                          + "'\n ALTER DATABASE " + Program.Db + " SET MULTI_USER";
                            MessageBox.Show(" " + strRestore, "", MessageBoxButtons.OK);
                            int Err = Program.ExecSqlNonQuery(strRestore, Program.connstr, "lỗi phục hồi cơ sở dữ liệu");
                            if (Err == 0)
                            {
                                int i;
                                this.PrgLoad.Visible = true;
                                this.PrgLoad.Minimum = 0;
                                this.PrgLoad.Maximum = 100;
                                this.PrgLoad.Step    = 20;
                                for (i = this.PrgLoad.Minimum; i <= this.PrgLoad.Maximum; i++)
                                {
                                    this.PrgLoad.Value = i;
                                    PrgLoad.PerformStep();
                                    Thread.Sleep(10);
                                }
                                PrgLoad.Visible = false;
                                MessageBox.Show(" Phục hồi thời gian lúc " + dt + " Thành công!", "", MessageBoxButtons.OK);
                            }
                            else
                            {
                                MessageBox.Show(" Sai đường dẫn backup . Kiểm tra lại  ", "", MessageBoxButtons.OK);
                            }
                        }

                        else
                        {
                            return;
                        }
                    }
                }
            }
        }
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Directory.Exists(Program.strDefaultPath) == true)
            {
                taoDevice();
            }
            else
            {
                OpenFileDialog folderBrowser = new OpenFileDialog();
                folderBrowser.ValidateNames   = false;
                folderBrowser.CheckFileExists = false;
                folderBrowser.CheckPathExists = true;
                folderBrowser.FileName        = "Folder Selection.";
                if (folderBrowser.ShowDialog() == DialogResult.OK)
                {
                    string folderPath = Path.GetDirectoryName(folderBrowser.FileName);
                    Program.strDefaultPathNew = folderPath + "\\";
                    strFullPathDevice         = Program.strDefaultPathNew + tenDevice + ".BAK";

                    String query = "USE master\n EXEC sp_addumpdevice '" + Program.device_type + "', '" + tenDevice + "','" + strFullPathDevice + "'";
                    try
                    {
                        Program.myreader = Program.ExecSqlDataReader(query);
                        if (Program.myreader != null)
                        {
                            int i;
                            this.PrgLoad.Visible = true;
                            this.PrgLoad.Minimum = 0;
                            this.PrgLoad.Maximum = 100;
                            this.PrgLoad.Step    = 10;

                            for (i = this.PrgLoad.Minimum; i <= this.PrgLoad.Maximum; i++)
                            {
                                PrgLoad.Value = i;

                                PrgLoad.PerformStep();
                                Thread.Sleep(10);
                            }

                            PrgLoad.Visible = false;
                            MessageBox.Show(" Tạo Device thành công!", "", MessageBoxButtons.OK);
                            this.backup_devicesTableAdapter.Fill(this.DS.backup_devices);
                            btnDevice.Enabled   = false;
                            btnSaoLuuAd.Enabled = true;
                            txtTenDevice.Text   = tenDevice;
                        }
                        else
                        {
                            MessageBox.Show(" Tạo Device thất bại!", "", MessageBoxButtons.OK);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error" + ex, "!", MessageBoxButtons.OK);
                        return;
                    }

                    Program.conn.Close();
                }
            }
        }