我收到此错误
服务器'XYZ-PC \ SQLEXPRESS'的备份失败。
如何解决?
//Define a Backup object variable.
Backup sqlBackup = new Backup();
//Specify the type of backup, the description, the name, and the database to be backed up.
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "BackUp of:" + "Garment.mdf" + "on" + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "FullBackUp";
sqlBackup.Database = "Garment.mdf";
//Declare a BackupDeviceItem
BackupDeviceItem deviceItem = new BackupDeviceItem("D:\\SQLBackup" + "Garment.bak", DeviceType.File);
//Define Server connection
ServerConnection connection = new ServerConnection(".\\SQLEXPRESS");
//To Avoid TimeOut Exception
Server sqlServer = new Server(connection);
sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
Database db = sqlServer.Databases["Garment.mdf"];
sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;
//Add the device to the Backup object.
sqlBackup.Devices.Add(deviceItem);
//Set the Incremental property to False to specify that this is a full database backup.
sqlBackup.Incremental = false;
sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
//Specify that the log must be truncated after the backup is complete.
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
sqlBackup.FormatMedia = false;
//Run SqlBackup to perform the full database backup on the instance of SQL Server.
sqlBackup.SqlBackup(sqlServer);
//Remove the backup device from the Backup object.
sqlBackup.Devices.Remove(deviceItem);
答案 0 :(得分:0)
您的数据库是否真的在服务器端调用garment.mdf
?那是逻辑数据库名称吗?
我觉得这不太可能。
如果要从服务器进行备份,则需要使用逻辑数据库名称(例如Garment
) - 但不实际的文件名!
所以试试这段代码:
// Define a Backup object variable.
Backup sqlBackup = new Backup();
// Specify the type of backup, the description, the name, and the database to be backed up.
sqlBackup.Action = BackupActionType.Database;
sqlBackup.BackupSetDescription = "Backup of 'Garment' on " + DateTime.Now.ToShortDateString();
sqlBackup.BackupSetName = "FullBackUp";
sqlBackup.Database = "Garment"; // use the **LOGICAL** database name here!
sqlBackup.Initialize = true;
sqlBackup.Checksum = true;
sqlBackup.ContinueAfterError = true;
// Declare a BackupDeviceItem
BackupDeviceItem deviceItem = new BackupDeviceItem("D:\\SQLBackup" + "Garment.bak", DeviceType.File);
// Define Server connection
ServerConnection connection = new ServerConnection(".\\SQLEXPRESS");
Server sqlServer = new Server(connection);
sqlServer.ConnectionContext.StatementTimeout = 60 * 60;
Database db = sqlServer.Databases["Garment"]; // use the LOGICAL database name!
// Add the device to the Backup object.
sqlBackup.Devices.Add(deviceItem);
// Set the Incremental property to False to specify that this is a full database backup.
sqlBackup.Incremental = false;
sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
// Specify that the log must be truncated after the backup is complete.
sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;
sqlBackup.FormatMedia = false;
// Run SqlBackup to perform the full database backup on the instance of SQL Server.
sqlBackup.SqlBackup(sqlServer);
// Remove the backup device from the Backup object.
sqlBackup.Devices.Remove(deviceItem);