使用C#& SMO,如何将备份设备添加到SQL Server?

时间:2010-03-18 18:15:04

标签: c# sql-server smo

我知道如何使用C#和SMO在SQL Server上创建文件备份:

public void BackupDatabase(Microsoft.SqlServer.Management.Smo.Server server, 
                               string databaseName, string backupFilename,
                               string backupName, string backupDescription) {

 Backup backup = new Backup();
 backup.Devices.AddDevice(backupFilename, DeviceType.File);
 backup.Database = databaseName;
 backup.Action = BackupActionType.Database;
 backup.BackupSetDescription = backupDescription;
 backup.BackupSetName = backupName;
 backup.Incremental = false;
 backup.LogTruncation = BackupTruncateLogType.Truncate;
 backup.Initialize = true; // supposed to overwrite
 backup.SqlBackup(server);
}

我不知道(并且找不到)的是如何将文件BackupDevice添加到SQL服务器。

1 个答案:

答案 0 :(得分:6)

使用BackupDevice.Create

Server server = new Server("localhost");
BackupDevice newDevice = new BackupDevice();
newDevice.Parent = server;
newDevice.Name = "newDevice";
newDevice.BackupDeviceType = BackupDeviceType.Disk;
newDevice.PhysicalLocation = "c:\temp\newdevice.bak";
newDevice.Create();