我正在尝试使用以下代码备份SQL Server数据库:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
string database = con.Database.ToString();
try
{
saveFileDialog1.Filter = "SQL Server database backup files|*.bak";
saveFileDialog1.Title = "Database Backup";
saveFileDialog1.FileName = "GestionStock.bak";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string sqlStmt = string.Format("USE MASTER BACKUP DATABASE [" + database +"] TO DISK='{0}'", saveFileDialog1.FileName);
using (SqlCommand bu2 = new SqlCommand(sqlStmt, con))
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
bu2.ExecuteNonQuery();
con.Close();
MessageBox.Show("Base de données sauvegardée avec success");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
此代码可以工作并创建备份文件但是当我尝试重新备份数据库时,我收到了以下错误消息:
如何解决?