我用数据库开发了一个Windows表单应用程序并发布了
当我试图获得使用代码实现的数据库备份时
string database = GetDataDirectory();
MessageBox.Show(database);
SqlConnection.ClearAllPools();
using (SqlConnection backupConn = new SqlConnection())
{
backupConn.ConnectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;";
backupConn.Open();
using (SqlCommand backupcomm = new SqlCommand())
{
backupcomm.Connection = backupConn;
backupcomm.CommandText = @"BACKUP DATABASE @DBNAME TO DISK='c:\\Users\Public\\BbCon.bak'";
backupcomm.Parameters.AddWithValue("@DBNAME", database+"\\BbCon.mdf");
int x = backupcomm.ExecuteNonQuery();
if(x == -1)
{
MessageBox.Show("DataBase Backup Created Successfully at c:\\Users\\Public");
}
}
backupConn.Close();
}
private string GetDataDirectory()
{
ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
return ad.DataDirectory;
//return Environment.CurrentDirectory.ToString();
}
但是当我导航到错误消息中显示的目录时,我发现了一个Bbcon.mdf文件但是我收到了错误
请帮我解决这个问题我遇到问题请帮帮我
答案 0 :(得分:1)
您无法通过其物理文件名备份数据库。您必须按数据库名称备份数据库:
BACKUP DATABASE [BbCon] TO DISK = 'c:\Users\Public\BbCon.bak'