如何使用代码备份SQL Server数据库


添加引用

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

代码

Backup bkpDatabase = new Backup();
// Set the backup type to a database backup
bkpDatabase.Action = BackupActionType.Database;
// Set the database that we want to perform a backup on
bkpDatabase.Database = DbName;// cmbDatabase.SelectedItem.ToString();

// Set the backup device to a file
//new BackupDeviceItem(saveBackupDialog.FileName, DeviceType.File);
BackupDeviceItem bkpDevice = new BackupDeviceItem(NewName, DeviceType.File);

// Add the backup device to the backup
bkpDatabase.Devices.Add(bkpDevice);
// Perform the backup
bkpDatabase.SqlBackup(SD.SrvSql);
File.Copy(Source, PathDestination + "\\" + fileName);

File.Delete(Source);

知识共享许可协议
《如何使用代码备份SQL Server数据库》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: