开发者

c# sql server backup percent read

sql command

BACKUP DATABASE [databasesample] TO  DISK = N'D:\kkk\test.bat' WITH  INIT ,  NOUNLOAD , 
NAME = N'name test',  NOSKIP ,  
STATS = 5,  DESCRIPTION = N'test',  NOFORMAT 

result

5 percent processed.
10 percent processed.
15 percent processed.
20 percent processed.
25 percent processed.
30 percent proce开发者_JS百科ssed.
35 percent processed.
40 percent processed.
45 percent processed.
50 percent processed.
55 percent processed.
60 percent processed.
65 percent processed.
70 percent processed.
75 percent processed.
80 percent processed.
85 percent processed.
90 percent processed.
95 percent processed.
Processed 30960 pages for database 'databasesample', file 'sample_Data' on file 1.
100 percent processed.

I want to catch the percentage with C#. I think BeginExecuteNonQuery() function is done. but I do not know exactly how. Suggestions?


Besides from using the SMO method as other have indicated, or from sneaking the percent_complete column of sys.dm_exec_requests, there is also another question asked here: how to I capture those 5 percent complete messages in C#?

The percent complete is an informational message. All informational messages in C# can be captured from the SqlConnection.InfoMessage event.


I would suggest using the SMO tools which contain a Backup class which sends a PercentCompleteNotification via a PercentComplete event. I love the SMO tools - they're powerful and easy to use.


What shaunmartin said!

Last time I did this, I used code somewhat like this. YMMV.

  Server server = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection));

  // Create a backup device
  Backup backup = new Backup();
  backup.Action = BackupActionType.Database;
  backup.Database = database;
  backup.Incremental = false;
  backup.Initialize = true;

  BackupDeviceItem backupDeviceItem = new BackupDeviceItem(backupPath, DeviceType.File);
  backup.Devices.Add(backupDeviceItem);

  backup.PercentCompleteNotification = 1;
  backup.Information += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(backup_Information);
  backup.PercentComplete += new PercentCompleteEventHandler(backup_PercentComplete);
  backup.Complete += new Microsoft.SqlServer.Management.Common.ServerMessageEventHandler(backup_Complete);

  backup.SqlBackupAsync(server);


Yes, as others have (now!) said, the SMO namespace is very useful. Here's a snippet from my backup application. it's in VB, not C#, and it runs the backup synchronously, unlike the earlier example, but it works well. We run it in a loop for all the 20-odd databases on our server. Very reliable.

Dim bkp As Backup = New Backup()
With bkp

    .Devices.AddDevice(Me.currentBackupFileName, DeviceType.File)

    .Database = conn.DatabaseName
    .Action = BackupActionType.Database
    .Initialize = True
    .PercentCompleteNotification = 20
    AddHandler .PercentComplete, AddressOf HandleBackupProgress
    .SqlBackup(svr)

    Me.backupFiles.Add(Me.currentBackupFileName)

End With

The handler receives a PercentCompleteEventArgs e as its argument, and you just pull the percentage complete out of it using e.Percent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜