Backup SQL Server database from windows form application by a button click [closed]

夙愿已清 提交于 2019-12-04 21:22:42

The following code backups the database 'TestDB' to the file 'E.\backupfile.Bak' if button 'button_backup' is clicked.

private void button_backup_Click(object sender, EventArgs e)
        {
            //con is the connection string
            con.Open();
            string str = "USE TestDB;";
            string str1="BACKUP DATABASE TestDB TO DISK = 'E:\\backupfile.Bak' WITH FORMAT,MEDIANAME = 'Z_SQLServerBackups',NAME = 'Full Backup of Testdb';";
            SqlCommand cmd1 = new SqlCommand(str, con);
            SqlCommand cmd2 = new SqlCommand(str1, con);
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
            MessageBox.Show("success");
            con.Close();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!