数据库不存在错误

当我进行数据库备份时,我得到一个错误,数据库不存在,但我可以很好地附加数据库和其他进程,如数据插入和更新工作正常。 但是,当我进行数据库备份时,它会给出以下错误。

我显示错误屏幕截图和备份按钮代码

string cnstr="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\fees_data.mdf;Integrated Security=True;User Instance=True;" SqlConnection connect; connect = new SqlConnection(cnstr); connect.Open(); if (txtdname.Text == "") { dname = "Default.bak"; } else { dname = txtdname.Text + ".bak"; } SqlCommand command; command = new SqlCommand(@"backup database fees_data to disk ='c:\DATABackup\" + dname + "'", connect); command.ExecuteNonQuery(); connect.Close(); 

当我单击备份按钮时,我收到错误:

“数据库’fee_data’不存在。请确保正确输入名称.BACKUP DATABASEexception终止。”

数据库名称可能与.mdf文件名不同。

运行此查询时会得到什么结果?

 select name from sys.databases; 

从那里使用正确的名称。

而不是这个代码

 SqlCommand command; command = new SqlCommand(@"backup database fees_data to disk ='c:\DATABackup\" + dname + "'", connect); command.ExecuteNonQuery(); 

使用下面写的代码

 string fullPath= ""; string executable = System.Reflection.Assembly.GetExecutingAssembly().Location; fullPath= (System.IO.Path.GetDirectoryName(executable)); AppDomain.CurrentDomain.SetData("DataDirectory", fullPath); fullPath=fullPath+"\\fees_data.mdf"; SqlCommand command; command = new SqlCommand(@"backup ['"+fullPath+"'] to disk ='c:\DATABackup\" + dname + "'", connect); command.ExecuteNonQuery();