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

1.添加引用

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
2.
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);

详细参见MSDN

https://msdn.microsoft.com/en-us/library/ms162133(v=sql.120).aspx

http://sridharbabuk.blogspot.com/2009/07/sql-server-database-backups-using-smo.html

posted @ 2015-09-07 16:36  hornet_team  阅读(469)  评论(1编辑  收藏  举报