部分源码如下:
Option ExplicitDim gSQLServer As SQLDMO.SQLServer'这一句定义我也明白,WidthEvents是什么意思????Dim WithEvents oBackupEvent As SQLDMO.Backup
Private Sub cmdBackup_Click()
On Error GoTo ErrHandler:
Dim oBackup As SQLDMO.Backup
gDatabaseName = cmbDatabaseName.Text
Set oBackup = New SQLDMO.Backup
'这一句我不懂哎呀。。是什么意思?是给oBackup对象增加一个事件吗? Set oBackupEvent = oBackup ' enable events
oBackup.Database = gDatabaseName
gBkupRstrFileName = txtDataFileName.Text
oBackup.Files = gBkupRstrFileName
' 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 Len(Dir(gBkupRstrFileName)) > 0 Then
Kill (gBkupRstrFileName)
End If
' Change mousepointer while trying to connect.
Screen.MousePointer = vbHourglass
' Backup the database.
oBackup.SQLBackup gSQLServer
' Change mousepointer back to the default after connect.
Screen.MousePointer = vbDefault
Set oBackupEvent = Nothing ' disable events
Set oBackup = Nothing
Exit SubErrHandler:
MsgBox "Error " & Err.Description
Resume Next
End SubPrivate Sub oBackupEvent_PercentComplete(ByVal Message As String, ByVal Percent As Long)
PrintStat "oBackupEvent_PercentComplete -- " & Message & " " & Percent
End Sub我想把它转成delphi的,备份部分已经实现,可就是进度条部分实现不了,主要是给oBackUp这个对象引入CompletePercent这个事件哪位兄弟帮忙一下。谢谢。。
Option ExplicitDim gSQLServer As SQLDMO.SQLServer'这一句定义我也明白,WidthEvents是什么意思????Dim WithEvents oBackupEvent As SQLDMO.Backup
Private Sub cmdBackup_Click()
On Error GoTo ErrHandler:
Dim oBackup As SQLDMO.Backup
gDatabaseName = cmbDatabaseName.Text
Set oBackup = New SQLDMO.Backup
'这一句我不懂哎呀。。是什么意思?是给oBackup对象增加一个事件吗? Set oBackupEvent = oBackup ' enable events
oBackup.Database = gDatabaseName
gBkupRstrFileName = txtDataFileName.Text
oBackup.Files = gBkupRstrFileName
' 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 Len(Dir(gBkupRstrFileName)) > 0 Then
Kill (gBkupRstrFileName)
End If
' Change mousepointer while trying to connect.
Screen.MousePointer = vbHourglass
' Backup the database.
oBackup.SQLBackup gSQLServer
' Change mousepointer back to the default after connect.
Screen.MousePointer = vbDefault
Set oBackupEvent = Nothing ' disable events
Set oBackup = Nothing
Exit SubErrHandler:
MsgBox "Error " & Err.Description
Resume Next
End SubPrivate Sub oBackupEvent_PercentComplete(ByVal Message As String, ByVal Percent As Long)
PrintStat "oBackupEvent_PercentComplete -- " & Message & " " & Percent
End Sub我想把它转成delphi的,备份部分已经实现,可就是进度条部分实现不了,主要是给oBackUp这个对象引入CompletePercent这个事件哪位兄弟帮忙一下。谢谢。。
解决方案 »
- 通过ADO以adLockOptimistic方式打开Access表后,所有编辑修改未经UpDate方法就立即显现且永久保存了。
- Word2003里office.CommandBarButton中定义的快捷键在word2007中没有了!急!
- 投票:你是否同意回复clear_zero的VB版主职位。
- VB的问题,解释一下代码
- 怎么样用VB写一个好的启动窗体
- 关于text中鼠标拖选的问题
- [紧急求助]关于对数据库操作的几个问题,谢谢!
- 请教大家一个关于菜单的问题????
- 如何使用ADO来创建与EXCEL的连接
- 关于时间字串的问题.(lovecat)
- 数据环境文件无法加载
- 跪求VB作POS系统 怎么对scanner编程
是自定义一个事件呀
VB的基本语法,你看看MSDN,或则找找书看看就知道了
Set oBackup = New SQLDMO.Backup
'这一句我不懂哎呀。。是什么意思?是给oBackup对象增加一个事件吗? Set oBackupEvent = oBackup ' enable events
这里不是说的很清楚吗,这是激活这个事件呀,不过好像标准的激活事件不是这么做的
Dim WithEvents oBackupEvent As SQLDMO.Backup
Dim WithEvents oRestoreEvent As SQLDMO.RestoreDim gbConnected As Boolean
Dim gDatabaseName As String
Dim gBkupRstrFileName As String
Dim gBkupRstrFilePath As StringConst gTitle = "Server Connection"Private Sub Form_Load()
Set gSQLServer = Nothing
optWinNTAuth.Value = True
gbConnected = False
WinNTAuthOptionsOn
buttonsConnectClosed
End SubPrivate Sub Form_Unload(Cancel As Integer)
If gbConnected = True Then
Call gSQLServer.DisConnect
End If
If Not gSQLServer Is Nothing Then
Set gSQLServer = Nothing
End If
End SubPrivate Sub cmdConnect_Click()
Dim ServerName As String
Dim UserName As String
Dim Password As String On Error GoTo ErrHandler: If gSQLServer Is Nothing Then
Set gSQLServer = New SQLDMO.SQLServer
End If
' Put text box values into connection variables.
ServerName = txtServerName.Text
UserName = txtUserName.Text
Password = txtPassword.Text
' Set the login timeout.
gSQLServer.LoginTimeout = 15
' Decision code for login authorization type: WinNT or SQL Server.
If optWinNTAuth.Value = True Then
gSQLServer.LoginSecure = True
End If
' Change mousepointer while trying to connect.
Screen.MousePointer = vbHourglass
gSQLServer.Connect ServerName, UserName, Password
gbConnected = True
' List all of the database names.
FillDatabaseList
' Change mousepointer back to the default after connect.
Screen.MousePointer = vbDefault
' Notify user that connection was successful.
MsgBox "Connection to server successful.", vbOKOnly, gTitle
buttonsConnectOpen
' Clear up the status text in the "result field".
txtStatus.Text = ""
Exit SubErrHandler:
MsgBox "Error " & Err.Description
' Change mousepointer back if it's hourglass.
If Screen.MousePointer = vbHourglass Then
Screen.MousePointer = vbDefault
End If
End SubPrivate Sub cmdDisconnect_Click()
On Error GoTo ErrHandler:
Dim Msg As String
Dim Response As String ' Disconnect from a connected server.
If gbConnected = True Then
Msg = "Disconnect from Server?"
Response = MsgBox(Msg, vbOKCancel, gTitle)
If Response = vbOK Then
Call gSQLServer.DisConnect
Set gSQLServer = Nothing
cmbDatabaseName.Clear
txtDataFileName.Text = ""
txtStatus.Text = ""
gbConnected = False
buttonsConnectClosed
End If
End If
Exit Sub
ErrHandler:
MsgBox "Error " & Err.Description
Resume Next
End SubPrivate Sub cmdBackup_Click()
On Error GoTo ErrHandler:
Dim oBackup As SQLDMO.Backup
gDatabaseName = cmbDatabaseName.Text
Set oBackup = New SQLDMO.Backup
Set oBackupEvent = oBackup ' enable events
oBackup.Database = gDatabaseName
gBkupRstrFileName = txtDataFileName.Text
oBackup.Files = gBkupRstrFileName
' 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 Len(Dir(gBkupRstrFileName)) > 0 Then
Kill (gBkupRstrFileName)
End If
' Change mousepointer while trying to connect.
Screen.MousePointer = vbHourglass
' Backup the database.
oBackup.SQLBackup gSQLServer
' Change mousepointer back to the default after connect.
Screen.MousePointer = vbDefault
Set oBackupEvent = Nothing ' disable events
Set oBackup = Nothing
Exit SubErrHandler:
MsgBox "Error " & Err.Description
Resume Next
End SubPrivate Sub cmdRestore_Click()
On Error GoTo ErrHandler:
Dim oRestore As SQLDMO.Restore
Dim Msg As String
Dim Response As String' Msg = "You must choose the right database name according to the data file name selected. Do you want to continue?"
' Response = MsgBox(Msg, vbYesNo, gTitle)
' If Response = vbNo Then
' Exit Sub
' End If
gDatabaseName = cmbDatabaseName.Text
Set oRestore = New SQLDMO.Restore
Set oRestoreEvent = oRestore ' enable events
oRestore.Database = gDatabaseName
gBkupRstrFileName = txtDataFileName.Text
oRestore.Files = gBkupRstrFileName
' Change mousepointer while trying to connect.
Screen.MousePointer = vbHourglass
' Restore the database.
oRestore.SQLRestore gSQLServer
' Change mousepointer back to the default after connect.
Screen.MousePointer = vbDefault
Set oRestoreEvent = Nothing ' disable events
Set oRestore = Nothing
Exit SubErrHandler:
MsgBox "Error " & Err.Description
Resume Next
End SubPrivate Sub cmdBrowse_Click()
On Error GoTo ErrHandler:
CommonDialog1.CancelError = True
CommonDialog1.Filter = "All Files (*.*)|*.*|Backup Files (*.bak)|*.bak"
CommonDialog1.FilterIndex = 2
CommonDialog1.InitDir = gBkupRstrFilePath
CommonDialog1.DefaultExt = "bak"
CommonDialog1.DialogTitle = "Data File Name:"
CommonDialog1.Action = 1
txtDataFileName.Text = CommonDialog1.FileName
Exit Sub
ErrHandler:
'User pressed the Cancel button
Exit Sub
End Sub' VB will create the right prototypes for you, if you select the oBackupEvent in
' the drop down listbox of your editor
Private Sub oBackupEvent_Complete(ByVal Message As String)
PrintStat "oBackupEvent_Complete -- " & Message
End SubPrivate Sub oBackupEvent_NextMedia(ByVal Message As String)
PrintStat "oBackupEvent_NextMedia -- " & Message
End SubPrivate Sub oBackupEvent_PercentComplete(ByVal Message As String, ByVal Percent As Long)
PrintStat "oBackupEvent_PercentComplete -- " & Message & " " & Percent
End SubPrivate Sub oRestoreEvent_Complete(ByVal Message As String)
PrintStat "oRestoreEvent_Complete -- " & Message
End SubPrivate Sub oRestoreEvent_NextMedia(ByVal Message As String)
PrintStat "oRestoreEvent_NextMedia -- " & Message
End SubPrivate Sub oRestoreEvent_PercentComplete(ByVal Message As String, ByVal Percent As Long)
PrintStat "oRestoreEvent_PercentComplete -- " & Message & " " & Percent
End SubPrivate Sub PrintStat(ByRef Message As String)
txtStatus.Text = txtStatus.Text + Message + vbCrLf
End Sub
Private Sub optSSAuth_Click()
If optSSAuth.Value = True Then
SSAuthOptionsOn
End If
End SubPrivate Sub optWinNTAuth_Click()
optWinNTAuth.Value = True
WinNTAuthOptionsOn
txtUserName.Text = ""
txtPassword.Text = ""
End SubPrivate Sub buttonsConnectClosed()
cmdConnect.Default = True
cmdConnect.Enabled = True
cmdBackup.Enabled = False
cmdRestore.Enabled = False
cmdDisconnect.Enabled = False
cmdBrowse.Enabled = False
cmbDatabaseName.Enabled = False
txtDataFileName.Enabled = False
' Enable the Authorization stuff.
optWinNTAuth.Enabled = True
optSSAuth.Enabled = True
txtServerName.Enabled = True
lblServer.Enabled = True
If optWinNTAuth = True Then
WinNTAuthOptionsOn
Else
SSAuthOptionsOn
End If
End SubPrivate Sub buttonsConnectOpen()
cmdConnect.Enabled = False
cmdBackup.Enabled = True
cmdRestore.Enabled = True
cmdDisconnect.Enabled = True
cmdBrowse.Enabled = True
cmbDatabaseName.Enabled = True
txtDataFileName.Enabled = True
' Disable the Authorization stuff.
optWinNTAuth.Enabled = False
optSSAuth.Enabled = False
txtServerName.Enabled = False
lblServer.Enabled = False
lblUserName.Enabled = False
lblPassword.Enabled = False
txtUserName.Enabled = False
txtPassword.Enabled = False
End SubPrivate Sub WinNTAuthOptionsOn()
lblUserName.Enabled = False
lblPassword.Enabled = False
txtUserName.Enabled = False
txtPassword.Enabled = False
End SubPrivate Sub SSAuthOptionsOn()
lblUserName.Enabled = True
lblPassword.Enabled = True
txtUserName.Enabled = True
txtPassword.Enabled = True
End SubPrivate Sub FillDatabaseList()
cmbDatabaseName.Clear
' Enumerate all of the databases and add the names to the list box.
Dim oDB As SQLDMO.Database
For Each oDB In gSQLServer.Databases
If oDB.SystemObject = False Then
cmbDatabaseName.AddItem oDB.Name
End If
Next oDB
' Take care of the assignment of gBkupRstrFilePath.
Dim MyPos As Integer
gBkupRstrFilePath = CurDir
MyPos = InStr(1, CurDir, "DevTools", 1)
If MyPos > 0 Then
gBkupRstrFilePath = Left(gBkupRstrFilePath, MyPos - 1)
If Len(Dir(gBkupRstrFilePath + "backup", vbDirectory)) Then
gBkupRstrFilePath = gBkupRstrFilePath + "backup\"
Else
gBkupRstrFilePath = "c:\"
End If
Else
gBkupRstrFilePath = "c:\"
End If
' Select the first database in the list.
If cmbDatabaseName.ListCount > 0 Then
cmbDatabaseName.ListIndex = 0
' Assign the default backup/restore file name.
If Len(cmbDatabaseName.Text) > 0 Then
txtDataFileName.Text = gBkupRstrFilePath + cmbDatabaseName.Text + ".bak"
End If
End If
End Sub
Private Sub cmbDatabaseName_Click()
' Assign the default backup/restore file name.
If Len(cmbDatabaseName.Text) > 0 Then
txtDataFileName.Text = gBkupRstrFilePath + cmbDatabaseName.Text + ".bak"
End If
End Sub