且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server使用C#执行备份

更新时间:2023-02-08 13:47:39

您可以并且很可能按照您的要求进行操作,

you can and its very possible to do what you asked for,

但是使用SMO自己进行备份不是很困难,但是困难的部分是管理备份和还原.

but doing the backup it self using SMO its not very hard, but the hard part is managing the backup and the restore.

很难将所有代码都放在这里,但是不合适.所以我会尽力把您需要的线放好.

it would be hard to put all the code here, but its wont fit. so I will try my best to put the lines you need.

SqlBackup.SqlBackup不返回任何值,它是一个void函数. 但是它需要一个参数"Server",请尝试以下代码:

SqlBackup.SqlBackup doesn't return any value, its a void function. but it takes one parameter which is "Server", try out the following code:

Server srvSql;

//Connect to Server using your authentication method and load the databases in srvSql
// THEN

Backup bkpDatabase = new Backup();
bkpDatabase.Action = BackupActionType.Database;
bkpDatabase.Incremental = true; // will take an incemental backup
bkpDatabase.Incremental = false; // will take a Full backup 
bkpDatabase.Database = "your DB name";
BackupDeviceItem bDevice = new BackupDeviceItem("Backup.bak", DeviceType.File);
bkpDatabase.Devices.Add(bDevice );

bkpDatabase.PercentCompleteNotification = 1;// this for progress
bkpDatabase.SqlBackup(srvSql);
bkpDatabase.Devices.Clear();