SQL Server使用C#执行备份 [英] SQL Server perform backup with C#

查看:148
本文介绍了SQL Server使用C#执行备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了使用C#通过SMO创建数据库备份的可能性. 这项任务很容易,代码也很简单.我只有一个问题:如何检查备份是否确实创建?

I've investigated the possibilities of creating database backups through SMO with C#. The task is quite easy and code straightforward. I've got only one question: how can I check if the backup was really created?

SqlBackup.SqlBackup 方法不返回任何参数,我什至不知道它是否抛出任何异常. (我唯一知道的是它正在阻塞,因为还有SqlBackupAsync方法)

SqlBackup.SqlBackup method returns no parameters and I don't even know if it throws any exceptions. (the only thing that I know is that it is blocking, because there's also SqlBackupAsync method)

我将不胜感激.

推荐答案

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

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

这篇关于SQL Server使用C#执行备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆