我从VB.NET调用SQLDMO8.0COM库(使用使用tlbimp生成的PIA ),以便使用百分比完成通知备份数据库:
Dim server As SQLDMO.SQLServer = Nothing
Dim backup As SQLDMO.Backup = Nothing
Dim restore As SQLDMO.Restore = Nothing
Dim backupAbortable As Boolean
Dim restoreAbortable As Boolean
Try
server = New SQLDMO.SQLServer
server.LoginSecure = True
server.Connect(serverName)
backup = New SQLDMO.Backup
backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database
backup.BackupSetDescription = "test"
backup.BackupSetName = "test"
backup.Database = databaseName
backup.Files = TransactSqlName.Delimit(fileName)
backup.TruncateLog = SQLDMO.SQLDMO_BACKUP_LOG_TYPE.SQLDMOBackup_Log_Truncate
backup.PercentCompleteNotification = 1
AddHandler backup.PercentComplete, AddressOf OnOperationPercentComplete
AddHandler backup.NextMedia, AddressOf OnOperationNextMedia
AddHandler backup.Complete, AddressOf OnOperationComplete
backupAbortable = True
backup.SQLBackup(server)
backupAbortable = False
restore = New SQLDMO.Restore
restore.Files = backup.Files
AddHandler restore.PercentComplete, AddressOf OnOperationPercentComplete
AddHandler restore.NextMedia, AddressOf OnOperationNextMedia
AddHandler restore.Complete, AddressOf OnOperationComplete
restoreAbortable = True
restore.SQLVerify(server)
restoreAbortable = False
server.DisConnect()
Catch ex As AbortException
If backupAbortable Then
backup.Abort()
End If
If restoreAbortable Then
restore.Abort()
End If
Finally
If restore IsNot Nothing Then
RemoveHandler restore.PercentComplete, AddressOf OnOperationPercentComplete
RemoveHandler restore.NextMedia, AddressOf OnOperationNextMedia
RemoveHandler restore.Complete, AddressOf OnOperationComplete
Marshal.FinalReleaseComObject(restore)
restore = Nothing
End If
If backup IsNot Nothing Then
RemoveHandler backup.PercentComplete, AddressOf OnOperationPercentComplete
RemoveHandler backup.NextMedia, AddressOf OnOperationNextMedia
RemoveHandler backup.Complete, AddressOf OnOperationComplete
Marshal.FinalReleaseComObject(backup)
backup = Nothing
End If
If server IsNot Nothing Then
Marshal.FinalReleaseComObject(server)
server = Nothing
End If
End Try除了事件处理程序之外,这很好--只有第一个连接起来的处理程序才会实际执行。我不能肯定地说NextMedia事件,因为它只为磁带备份触发,但是对于另外两个事件,我要么得到完整的事件,要么根据AddHandler语句的顺序得到PercentComplete事件触发,而不是两者同时发生。
可能性:
我所做的一切都是错误的(建议在SQLDMO8.0中welcome!)
)
有什么想法吗?
发布于 2009-06-18 03:05:45
这在VB.NET 2005中是可行的,不确定在1.1左右。
我知道您已经在COM互操作上做了大量的工作,但是您能不能直接使用Info来连接
注意STATS = 10.这意味着每10%发出进度通知。
这只是我一直在做的一个项目的一个片段,我希望你能从中得到你需要的东西。
public sub Backup()
Dim Conn As SqlClient.SqlConnection
dim theCommand as SqlClient.SQLCommand
Conn = New SqlClient.SqlConnection("Data Source=.\MyInstance;Initial Catalog=Master;Integrated Security=SSPI;")
theCommand = Conn.CreateCommand
theCommand.CommandText = "BACKUP DATABASE [MyDatabase] TO DISK = '" & mDatabasePath & "\" & Filenames.SQLBackup & "' WITH NOFORMAT, INIT, NAME = 'MyDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10"
AddHandler Conn.InfoMessage, AddressOf onSqlInfoMessage
'make sure the events are fired as they are available, instead of at the end.
Conn.FireInfoMessageEventOnUserErrors = True
Conn.Open()
theCommand.ExecuteNonQuery()
RemoveHandler Conn.InfoMessage, AddressOf onSqlInfoMessage
Conn.Close()
end sub
Private Sub onSqlInfoMessage(ByVal sender As Object, ByVal args As SqlClient.SqlInfoMessageEventArgs)
If args.Message.EndsWith("percent processed.") Then
Dim theMatch As System.Text.RegularExpressions.Match
theMatch = mRegEx.Match(args.Message)
debug.print("Progress = " & theMatch.Value.Trim)
End If
End Subhttps://stackoverflow.com/questions/961874
复制相似问题