通过AttachDBFilename备份恢复sql数据库
我无法创建保存在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先 - 我猜你的一些参数是错误的:
在这里,你需要传递仅服务器名称 - 所以在你的情况下,不要发送整个连接字符串 - 只需
。 \SQLExpress
至于你的数据库名称 - 我不知道你是否可以使用 SMO 来备份 SQL Server 中的“附加”MDF 文件 - 通常,这将是数据库名称(数据库的名称 仅 - 无文件名,无扩展名)当数据库在服务器上时。
所以我的建议是:将此
mydb
然后仅使用数据库名称 作为您的值:
有了这些点,您的代码在我的情况下就可以正常工作,至少......
First of all - I guess some of your parameters are wrong:
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.
So my suggestion here would be:
mydb
then use just the database name as your value here:
With these points in place, your code does work just fine in my case, at least...