通过AttachDBFilename备份恢复sql数据库

发布于 2024-12-01 23:10:56 字数 1528 浏览 2 评论 0原文

我无法创建保存在 C:\database\mydb.mdf 等位置的数据库备份

错误:无法创建备份

        Backup sqlBackup = new Backup();

        sqlBackup.Action = BackupActionType.Database;
        sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
                                         DateTime.Now.ToShortDateString();
        sqlBackup.BackupSetName = "Archive";

        sqlBackup.Database = databaseName;

        BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
        //ServerConnection connection = new ServerConnection(serverName, userName, password);
        ServerConnection connection = new ServerConnection(serverName);

        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases[databaseName];

        sqlBackup.Initialize = true;
        sqlBackup.Checksum = true;
        sqlBackup.ContinueAfterError = true;

        sqlBackup.Devices.Add(deviceItem);
        sqlBackup.Incremental = false;

        sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

        sqlBackup.FormatMedia = false;

        sqlBackup.SqlBackup(sqlServer);   

 string dataBaseName = @"C:\database\mydb.mdf";    
 string serverName = @"Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True";    
 string destinationPath = "C:\\mydb.bak";    

也许我传递了错误的变量?

请任何人验证它并

提前向我发布正确的解决方案。

PS:数据库没有密码保护,可以使用混合身份验证

I am not able to create a backup of database saved in location like C:\database\mydb.mdf

error : Unable to create a backup

        Backup sqlBackup = new Backup();

        sqlBackup.Action = BackupActionType.Database;
        sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
                                         DateTime.Now.ToShortDateString();
        sqlBackup.BackupSetName = "Archive";

        sqlBackup.Database = databaseName;

        BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
        //ServerConnection connection = new ServerConnection(serverName, userName, password);
        ServerConnection connection = new ServerConnection(serverName);

        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases[databaseName];

        sqlBackup.Initialize = true;
        sqlBackup.Checksum = true;
        sqlBackup.ContinueAfterError = true;

        sqlBackup.Devices.Add(deviceItem);
        sqlBackup.Incremental = false;

        sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

        sqlBackup.FormatMedia = false;

        sqlBackup.SqlBackup(sqlServer);   

 string dataBaseName = @"C:\database\mydb.mdf";    
 string serverName = @"Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True";    
 string destinationPath = "C:\\mydb.bak";    

Maybe I am passing wrong variables?

Please can anyone verify it and post me the right solution

thnx in advance.

PS: database is not password protected and can use mixed authentication

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2024-12-08 23:10:56

首先 - 我猜你的一些参数是错误的:

ServerConnection connection = new ServerConnection(serverName);

在这里,你需要传递仅服务器名称 - 所以在你的情况下,不要发送整个连接字符串 - 只需 。 \SQLExpress

至于你的数据库名称 - 我不知道你是否可以使用 SMO 来备份 SQL Server 中的“附加”MDF 文件 - 通常,这将是数据库名称(数据库的名称 - 无文件名,无扩展名)当数据库在服务器上时。

string dataBaseName = @"C:\database\mydb.mdf";    

所以我的建议是:将此

  • MDF 文件附加到您的 SQL Server 实例(您已经安装了该实例)
  • 并为其指定一个有意义的名称,例如 mydb
  • 然后仅使用数据库名称 作为您的值:

    字符串 dataBaseName = "mydb";      
    

有了这些点,您的代码在我的情况下就可以正常工作,至少......

First of all - I guess some of your parameters are wrong:

ServerConnection connection = new ServerConnection(serverName);

Here, you need to pass just the server's name - so in your case, do not send in your whole connection string - just .\SQLExpress

As for your database name - I don't know if you can use SMO to backup an "attached" MDF file in SQL Server - normally, this would be the database name (the name of the database only - no file name, no extension) when the database is on the server.

string dataBaseName = @"C:\database\mydb.mdf";    

So my suggestion here would be:

  • attach this MDF file to your SQL Server instance (which you have installed anyway)
  • give it a meaningful name, e.g. mydb
  • then use just the database name as your value here:

    string dataBaseName = "mydb";      
    

With these points in place, your code does work just fine in my case, at least...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文