Twitter Delicious Facebook Digg Stumbleupon Favorites More

Thứ Ba

Backup và restore DB SQL trong lập trình C#

private void btRestore_Click(object sender, Ev entArgs e)
        {
            try
            {  
                string path = "D:\\backupUITGPP.bak";
                string sqlRestore  = "Use master Restore Database [UITGPP] from disk='" + path + "'";
                SqlConnection con  = new SqlConnection("Data Source=ACUMEN-PC\\SQLEXPRESS;Initial Catalog=Ketnoimoi;Integrat ed Security=True");
                con.Open();
               
                SqlCommand cmd =  new SqlCommand(sqlRestore, con);
                cmd.ExecuteNonQuer y();
                con.Close();
                MessageBox.Show("Database da duoc restore ");

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex .Message, "Restore Database");
                return;
            }
            catch (Exception ex)
            {

            }

        }


Backup:

private void btBackUp_Click(object sender, Eve ntArgs e)
        {
            try
            {
                string sqlBackup = "BACKUP DATABASE [UITGPP] TO DISK='D:\\backupUITGPP.bak'";
                SqlConnection con =new SqlConnection("Data Source=ACUMEN-PC\\SQLEXPRESS;Initial Catalog=UITGPP;IntegratedSecurity=True");
                con.Open();
                SqlCommand cmd =  new SqlCommand(sqlBackup, con);
                cmd.ExecuteNonQuer y();
                con.Close();
                MessageBox.Show("Đã backup cơ sở dữ liệu");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex .Message, "Backup Database");
                return;
            }
            catch (Exception ex)
            {

            }

        }

Nguồn: congdongcviet.com

0 nhận xét:

Đăng nhận xét

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates