BackupDevice.物理分配不添加到指定的位置
本文关键字:位置 添加 分配 BackupDevice | 更新日期: 2023-09-27 18:15:38
使用c#和SMO,当我创建备份时,它们被复制到SQL Server使用的默认备份位置(C:'Program Files'Microsoft SQL Server'MSSQL11.SQLEXPRESS'MSSQL' backup),而不是我在代码中指定的物理位置:
Database database = Server.Databases[dbName]);
Backup backup = new Backup();
device = new BackupDevice();
device.Parent = Server;
device.Name = dbName + ".bak";
device.BackupDeviceType = BackupDeviceType.Disk;
device.PhysicalLocation = Path.Combine(filePath + device.Name); // doesn't appear to do anything
device.Create();
backup.Action = BackupActionType.Database;
backup.Database = database.Name;
backup.Devices.AddDevice(filePath, DeviceType.File);
backup.SqlBackup(server);
当我运行我的代码时,我发现我指定的路径("C:'backupTest")是空的,并且备份已被添加到默认的备份位置。
有谁知道这是为什么吗?试试下面的代码
static void BackupDataBase(string databaseName, string destinationPath)
{
try
{
Server myServer = GetServer();
Backup backup = new Backup();
backup.Action = BackupActionType.Database;
backup.Database = databaseName;
destinationPath = System.IO.Path.Combine(destinationPath, databaseName + ".bak");
backup.Devices.Add(new BackupDeviceItem(destinationPath, DeviceType.File));
backup.Initialize = true;
backup.Checksum = true;
backup.ContinueAfterError = true;
backup.Incremental = false;
backup.LogTruncation = BackupTruncateLogType.Truncate;
backup.SqlBackup(myServer);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static Server GetServer()
{
ServerConnection conn = new ServerConnection("server", "username", "pw");
Server myServer = new Server(conn);
return myServer;
}
有关更多信息,请参阅这篇代码项目文章。