Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server perform backup with C#

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.

like image 665
kubal5003 Avatar asked May 03 '11 14:05

kubal5003


1 Answers

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:

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();
like image 178
Arrabi Avatar answered Nov 14 '22 08:11

Arrabi