The progress bar is not displayed when the SMO is used to back up the database, that is, the progress bar event PercentComplete is not triggered.
I tried it today. It's strange.
The Code is as follows:
Using Microsoft. SqlServer. Management. Smo;
Using Microsoft. SqlServer. Management. Common;
Private void btnBackup_Click (object sender, EventArgs e)
{
BtnBackup. Enabled = false;
Thread tr = new Thread (new ThreadStart (doBackup ));
Tr. Priority = ThreadPriority. AboveNormal;
Tr. Start ();
// Thread. Sleep (3000 );
}
/// <Summary>
/// Back up the database
/// </Summary>
Public void doBackup ()
{
PbDemo. Value = 0;
PbDemo. Maximum = 100;
PbDemo. Style = ProgressBarStyle. Blocks;
// PbDemo. Step = 10;
Server srv = new Server (@ "(local )");
Backup backup = new Backup ();
Backup. Action = BackupActionType. Database;
Backup. Database = "btnet ";
Backup. Incremental = false;
Backup. Devices. Add (new BackupDeviceItem (@ "C: \ agronet09.bak", DeviceType. File ));
Backup. Initialize = true;
Backup. PercentCompleteNotification = 10;
Backup. PercentComplete + = new PercentCompleteEventHandler (backup_PercentComplete );
// Backup. Checksum = true;
Backup. SqlBackup (srv );
}
Public void backup_PercentComplete (object sender, Microsoft. SqlServer. Management. Smo. PercentCompleteEventArgs e)
{
This. Invoke (new displayProgress_delegate (displayProgress), e. Percent );
// Application. DoEvents ();
}
Public delegate void displayProgress_delegate (int progress );
Public void displayProgress (int progress)
{
This. lbProgress. Text = "completed [" + progress. ToString () + "%]";
PbDemo. Value = progress;
BtnBackup. Enabled = (progress = 100 );
}
The results are correctly executed, but the progress bar is not displayed.
At first, I thought it was a thread problem. Later I found it was not the cause.
I tried another piece of code.
Using Microsoft. SqlServer. Management. Smo;
Using Microsoft. SqlServer. Management. Common;
Using System. Diagnostics;
Static void Main (string [] args)
{
BackupDatabase ("region \ agronet09", "agronet2008", "c: \ Northind_3.bak ");
Console. WriteLine (Environment. NewLine + "Press any key to continue .");
Console. ReadKey ();
}
Public static void BackupDatabase (string serverName, string databaseName, string fileName)
{
Console. WriteLine ("*** Backing up ***");
Server server = new Server (serverName );
Backup backup = new Backup ();
Backup. Action = BackupActionType. Database;
Backup. Database = databaseName;
Backup. Incremental = false;
Backup. Initialize = true;
Backup. LogTruncation = BackupTruncateLogType. Truncate;
BackupDeviceItem backupItemDevice = new BackupDeviceItem (fileName, DeviceType. File );
Backup. Devices. Add (backupItemDevice );
Backup. PercentCompleteNotification = 10;
Backup. PercentComplete + = backup_PercentComplete;
Backup. Complete + = backup_Complete;
Backup. SqlBackup (server );
}
Protected static void backup_PercentComplete (object sender, PercentCompleteEventArgs e)
{
Console. WriteLine (e. Percent + "% processed .");
// Application .();
System. Threading. Thread. Sleep (1000 );
}
Protected static void backup_Complete (object sender, ServerMessageEventArgs e)
{
Console. WriteLine (Environment. NewLine + e. ToString ());
}
The result is not displayed.
Later, I suddenly thought about whether the file was too small. I tried a m data file and it was displayed successfully:
After repeated experiments, we found that SQL server 2000 displays a progress bar when the data file is added with a log file larger than 6 MB.
The SQL server 2008 r2 displays a progress bar at about 3 m. It may be related to machines.
Note: SQL server 2008 r2 requires that the primary data file must be greater than 3 MB!
References:
Http://msdn.microsoft.com/zh-cn/magazine/cc163409.aspx
DEMO code download