SQLDMO.Backup and ProgressBar

G

Guest

Hi all
My requirement is to "on button_click, backup a SQL
database using SQLDMO.Backup object and update the
progress in a ProgressBar. The problem is the progress
bar does not update at all until the very end of the
backup process, when the backup is almost going to be
over, the progress bar updates itself once on a stretch,
which is no use,it is not showing any progress.

SQLDMO documentation says, the SQLDMO.Backup object fires
PercentComplete event every 10% of the process. I
appreciate all your help in figuring this out. Thanks a
lot.

private void button1_Click(object sender,
System.EventArgs e)
{
string DatabaseName = "MySqlServerDB";
string BackupFileName = PathFileName.Text;
string Prefix = " button1_Click: ";

//check if the text box contains a valid backup
name and path
if(!PathFileName.Text.EndsWith(".bak"))
{
MessageBox.Show("Enter a valid Backup
path and file name. ");
goto ExitClick;
}

string BackupDir = Path.GetDirectoryName
(BackupFileName);
if(!Directory.Exists(BackupDir))
{
MessageBox.Show("Invalid Path
specified. ");
goto ExitClick;
}
if(!JobHelperObj.CheckDiskSpace(BackupDir))
{
MessageBox.Show("Insufficient disk space
in the drive. Backup will not continue. ");
goto ExitClick;
}

this.Cursor = Cursors.WaitCursor;
progressBar1.Visible = true;

try
{
//declare an instance of SQLDMO.Backup
SQLDMO.Backup DMOBackup = new
SQLDMO.BackupClass();
SQLDMO.SQLServer SqlServerObj = new
SQLDMO.SQLServerClass();
SqlServerObj.Name = ".";
SqlServerObj.LoginSecure = true;
SqlServerObj.Connect(".",null,null);

DMOBackup.Database = DatabaseName;
DMOBackup.Files = BackupFileName;

// Delete the datafile to allow the
application to create a brand new file.
// This will prevent attaching the new
backup data to the old data if there
// is any.
if(File.Exists(BackupFileName))
File.Delete(BackupFileName);

DMOBackup.PercentComplete += new

SQLDMO.BackupSink_PercentCompleteEventHandler
(this.SqlBackupPercentComplete);

DMOBackup.SQLBackup(SqlServerObj);
MessageBox.Show("Backup complete");

this.Cursor = Cursors.Default;
progressBar1.Visible = false;
progressBar1.Value = 0;

DMOBackup = null;
}
catch(Exception excep)
{
TraceMessage = " ERROR: " + Prefix +
excep.Message + ". " + e.GetType().ToString() + ". " +
" Exception occurred. ";
ErrorMessage = " ERROR: Unable to get the
BackupPath from DAHS table. ";
Log(TraceMessage);
Log(ErrorMessage, true);
MessageBox.Show(excep.Message + " Backup failed.
One of the possible causes could be Insufficient disk
space. ");
}
ExitClick:;
}

private void SqlBackupPercentComplete(string message, int
Percent)
{
progressBar1.Value = Percent;
progressBar1.Update();
}
 
T

The Last Gunslinger

Hi all
My requirement is to "on button_click, backup a SQL
database using SQLDMO.Backup object and update the
progress in a ProgressBar. The problem is the progress
bar does not update at all until the very end of the
backup process, when the backup is almost going to be
over, the progress bar updates itself once on a stretch,
which is no use,it is not showing any progress.

Have you tried debugging it to see how many times the event fires?
Try writing to the debug stream (debug.WriteLine("Text")) in the
eventhandler and see what happens.

Post back with results if required.

HTH
JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top