Using C# & SMO, how do I add a backup device to a SQL Server?

老子叫甜甜 提交于 2020-01-24 04:53:30

问题


I know how to use C# and SMO to create a file backup on a 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);
}

What I don't know (and can't find) is how to add the file BackupDevice to the SQL server.


回答1:


Use 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();


来源:https://stackoverflow.com/questions/2472316/using-c-sharp-smo-how-do-i-add-a-backup-device-to-a-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!