Exemplo n.º 1
0
 private void tsbsaoluu_Click(object sender, EventArgs e)
 {
     if (saoluu == true)
     {
         //saoluu
         if (!string.IsNullOrEmpty(txtlayfile.Text))
         {
             if (File.Exists(txtlayfile.Text))
             {
                 File.Delete(txtlayfile.Text);
             }
             lblerr.Text = "Hệ thống đang tiến hành sao lưu...";
             Application.DoEvents();
             try
             {
                 //thực hiện thủ tục sao lưu
                 if (bd.SaoLuu(ref err, txtlayfile.Text))
                 {
                     lblerr.Text      = "Đã sao lưu thành công";
                     lblerr.ForeColor = Color.Red;
                 }
                 else
                 {
                     lblerr.Text      = "Sao lưu không thành công" + err;
                     lblerr.ForeColor = Color.Red;
                 }
             }
             catch (Exception ex)
             {
                 err              = ex.Message;
                 lblerr.Text      = "Sao lưu không thành công" + err;
                 lblerr.ForeColor = Color.Red;
             }
         }
         else
         {
             MessageBox.Show("Chưa có đường dẫn file bak", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
     else
     {
         //phục hội
         if (!string.IsNullOrEmpty(txtlayfile.Text))
         {
             lblerr.Text = "Hệ thống đang tiến hành phục hồi...";
             Application.DoEvents();
             try
             {
                 //thực hiện thủ tục sao lưu
                 dataname
                 if (bd.PhucHoi(ref err, txtlayfile.Text, databasename))
                 {
                     lblerr.Text      = "Đã Phục hồi thành công";
                     lblerr.ForeColor = Color.Red;
                 }
                 else
                 {
                     lblerr.Text      = "Phục hồi không thành công" + err;
                     lblerr.ForeColor = Color.Red;
                 }
             }
             catch (Exception ex)
             {
                 err              = ex.Message;
                 lblerr.Text      = "Phục hồi không thành công" + err;
                 lblerr.ForeColor = Color.Red;
             }
         }
         else
         {
             MessageBox.Show("Chưa có đường dẫn file bak", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
 }
        private void button2_Click(object sender, EventArgs e)
        {
            if (ckbPhucHoi.Checked)
            {
                //Phuc Hoi
                lblErr.Text      = "Đang phục hồi dữ liệu...";
                lblErr.ForeColor = Color.Red;
                Application.DoEvents();
                string err = "";
                try
                {
                    string sql = string.Format("USE Master \n ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE" + " RESTORE DATABASE {1} FROM DISK = N'{2}' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10" + " ALTER DATABASE {3} SET MULTI_USER", Cls_Main.connectionStringBuilder.InitialCatalog, Cls_Main.connectionStringBuilder.InitialCatalog, txtPath.Text, Cls_Main.connectionStringBuilder.InitialCatalog);
                    if (bd.PhucHoi(sql, ref err, ref rows))
                    {
                        lblErr.Text      = "Đã phục hồi thành công";
                        lblErr.ForeColor = Color.White;
                    }
                    else
                    {
                        lblErr.Text      = "Phục hồi không thành công" + err;
                        lblErr.ForeColor = Color.White;
                    }
                }
                catch (Exception ex)
                {
                    err              = ex.Message;
                    lblErr.Text      = "Sao lưu không thành công" + err;
                    lblErr.ForeColor = Color.Blue;
                }
            }
            else
            {
                lblErr.Text      = "Đang sao lưu dữ liệu...";
                lblErr.ForeColor = Color.Red;
                Application.DoEvents();
                if (File.Exists(txtPath.Text))
                {
                    File.Delete(txtPath.Text);
                }

                try
                {
                    if (bd.SaoLuu(ref err, ref rows, txtPath.Text))
                    {
                        lblErr.Text      = "Đã sao lưu thành công";
                        lblErr.ForeColor = Color.White;
                    }
                    else
                    {
                        lblErr.Text      = "Sao lưu không thành công" + err;
                        lblErr.ForeColor = Color.White;
                    }
                }
                catch (Exception ex)
                {
                    err              = ex.Message;
                    lblErr.Text      = "Sao lưu không thành công." + err;
                    lblErr.ForeColor = Color.Blue;
                }
            }
        }