码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-09-07 16:49:23      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/hornet/p/4789245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!