【问题标题】:Database does not exist error数据库不存在错误
【发布时间】:2012-09-17 06:40:49
【问题描述】:

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

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

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();

当我点击备份按钮时出现错误:

“数据库‘fees_data’不存在。确保名称输入正确。 BACKUP DATABASE 异常终止。"

【问题讨论】:

  • 这将起作用,您只需符合数据库名称和路径,而不是任何代码错误,只需检查您的数据库名称和存储备份文件的路径...

标签: c# sql-server


【解决方案1】:

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

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

select name from sys.databases;

从那里使用正确的名称。

【讨论】:

    【解决方案2】:

    代替这段代码

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2016-09-14
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2013-12-10
      相关资源
      最近更新 更多