【问题标题】:How to backup from .mdf database that created by VS如何从 VS 创建的 .mdf 数据库进行备份
【发布时间】:2012-03-18 20:49:14
【问题描述】:

我使用 vs 2010 创建了一个 .mdf 数据库文件。我可以检索数据并将其插入到数据库中,但是当我要备份时,错误已得到处理。该数据库未附加在 Management Studio 中。

我的代码:

SqlConnection connect;
connect = new SqlConnection(DAL.AccessLayerClass._connectionStr);
connect.Open();
SqlCommand command;
command = new SqlCommand(@"backup database AGMDB to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
command.ExecuteNonQuery();
connect.Close();
MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);

我的连接字符串:

string _connectionStr = "Data Source=.\\SQLEXPRESS; AttachDbFilename=" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True;";

并且发生了该错误:

Could not locate entry in sysdatabases for database 'AGMDB'. No entry found with that name. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.

我该如何解决这个错误? 谢谢

【问题讨论】:

    标签: c# backup sql-server-express


    【解决方案1】:

    DBFileName 周围需要[],试试这个:

    SqlConnection connect;
    connect = new SqlConnection(DAL.AccessLayerClass._connectionStr);
    connect.Open();
    SqlCommand command;
    command = new SqlCommand(@"backup database [" + System.Windows.Forms.Application.StartupPath + "\\Database\\AGMDB.mdf] to disk ='d:\svBackUp1.bak' with init,stats=10",connect);
    command.ExecuteNonQuery();
    connect.Close();
    MessageBox.Show("The support of the database was successfully performed", "Back", MessageBoxButtons.OK, MessageBoxIcon.Information);
    

    参考:How to Backup and Restore SQL Express 2005 (AttachDbFilename mode)

    【讨论】:

    • 不要乞求赞成票。此外,仅发布一个链接并不能作为一个好的答案,请查看此常见问题解答条目以获取指导:How do I write a good answer to a question?。除非是为了避免歧义,否则标识符周围的方括号不是强制性的。
    • 没问题,如果你稍微修正一下你的答案,也许可以从你链接的页面中添加一段摘录,然后我将取消我的反对票。
    猜你喜欢
    • 2011-04-07
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2017-01-07
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多