SQL Server使用C#执行备份
我研究了使用 C# 通过 SMO 创建数据库备份的可能性。 该任务非常简单,代码也很简单。我只有一个问题:如何检查备份是否真正创建?
SqlBackup.SqlBackup 方法返回没有参数,我什至不知道它是否抛出任何异常。 (我唯一知道的是它是阻塞的,因为还有 SqlBackupAsync 方法)
我将不胜感激。
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 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)
I would appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以并且很有可能按照您的要求进行操作,
但是使用 SMO 自行进行备份并不是很困难,但困难的部分是管理备份和恢复。
很难将所有代码放在这里,但它不适合。所以我会尽力把你需要的台词放进去。
SqlBackup.SqlBackup 不返回任何值,它是一个 void 函数。
但它需要一个参数“Server”,请尝试以下代码:
you can and its very possible to do what you asked for,
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 doesn't return any value, its a void function.
but it takes one parameter which is "Server", try out the following code:
我已经使用 Reflector.NET 调查了这个问题(我认为这是合法的,因为 RedGate 是女士金牌认证合作伙伴,并且 Reflector.NET 打开开箱即用的 .NET 库)。正如我发现该方法抛出两种类型的异常:
FailedOperationException - 在大多数情况下,其他异常都是“翻译”的(我想翻译意味着创建新的 FailedOperationException 并将 InnerException 设置为实际抛出的异常)
UnsupportedVersionException - 在一种情况下,当日志截断设置为 TruncateOnly 并且服务器主要版本大于或等于 10(这是 sql server 2008?)
这部分解决了我的问题,因为我不能 100% 确定如果出现问题,这些异常实际上会被抛出。
I've investigated the problem using Reflector.NET (I suppose this is legal since RedGate is Ms Gold Certified Partner and Reflector.NET opens .NET libraries out of the box). As I found out the method throws two types of exceptions:
FailedOperationException - in most cases, other exceptions are "translated" (I suppose translating means creating new FailedOperationException and setting InnerException to what was actually thrown)
UnsupportedVersionException - in one case when log truncation is set to TruncateOnly and server major version is more or equal to 10 (which is sql server 2008?)
This solves my problem partially, because I'm not 100% sure that if something goes wrong those exceptions will actually be thrown.