SQL Server backs up using C #

I explored the possibilities of backing up databases through SMO using C #. The task is quite simple and simple. I have only one question: how can I check if the backup is really created?

The SqlBackup.SqlBackup method does not return any parameters, and I don’t even know if it throws any exceptions. (the only thing I know is that it blocks because there is also the SqlBackupAsync method)

I would appreciate any help.

+6
source share
2 answers

you can and very possibly do what you asked,

but doing backup yourself using SMO is not very difficult, but the hard part is managing the backup and recovery.

it would be difficult to put all the code here, but it won't work. so I will try to do everything you need.

SqlBackup.SqlBackup does not return any value; its function is void. but one server parameter is required, try 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(); 
+1
source

I investigated the problem using Reflector.NET (I believe this is legal, since RedGate is a Gold Gold certified partner, and Reflector.NET opens the .NET libraries out of the box). As it turned out, the method throws two types of exceptions:

FailedOperationException - in most cases, other exceptions are "thrown" (I assume that translating means creating a new FailedOperationException and setting an InnerException to what was actually thrown)

UnsupportedVersionException - in one case, when log truncation is set to TruncateOnly, and the main version of the server is greater than or equal to 10 (is it SQL Server 2008?)

This partially solves my problem because I am not 100% sure that if something goes wrong, these exceptions will indeed be thrown.

0
source

Source: https://habr.com/ru/post/887205/


All Articles