So last night during the geek night session at the SDC, the Dutch, inspired by Richard Campbell called me on my SMO Backup and Restore GUI that had a progress meter. They thought I was hacking it, not that I was actually providing a true representation of the progress made by the status of the backup. Here is the progress meter in action, as the database backup makes progress we update the progress meter:
To do a backup programmatically you can to use SMO (see yesterday). Begin by setting the variables to get started.
Server svr = new Server();//assuming local server
Backup bkp = new Backup();
Cursor = Cursors.WaitCursor;
Then you have to set the device to backup to and what database to backup. Notice in the comments the code to the progress meter
try
{
string strFileName = txtFileName.Text.ToString();
string strDatabaseName = txtDatabase.Text.ToString();
bkp.Action = BackupActionType.Database;
bkp.Database = strDatabaseName;
//set the device: File, Tape, etc
bkp.Devices.AddDevice(strFileName, DeviceType.File);
//set this when you want to do Incremental
bkp.Incremental = chkIncremental.Checked;
//progress meter stuff
progressBar1.Value = 0;
progressBar1.Maximum = 100;
progressBar1.Value = 10;
//this gives us the % complete by handling the event
//provided by SMO on the percent complete, we will
//update the progress meter in the event handler
//set the progress meter to 10% by default
bkp.PercentCompleteNotification = 10;
//call to the event handler to incriment the progress meter
bkp.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
//this does the backup
bkp.SqlBackup(svr);
//alert the user when it is all done
MessageBox.Show("Database Backed Up To: " + strFileName, "SMO Demos");
}
catch (SmoException exSMO)
MessageBox.Show(exSMO.ToString());
catch (Exception ex)
MessageBox.Show(ex.ToString());
finally
Cursor = Cursors.Default;
Here is the ProgressEventHandler, notice that I made it generic enough that I can call it from both the backup and restore methods!
public void ProgressEventHandler(object sender, PercentCompleteEventArgs e)
//increase the progress bar up by the percent
progressBar1.Value = e.Percent;
Page rendered at Thursday, March 30, 2023 10:26:43 AM (Eastern Standard Time, UTC-05:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.