我老师给了我一段数据库备份的程序,但是很多地方我都看不懂,下面帖出来,请高手指点.
Option Explicit
Private strBackupPath As String '备份路径
Private strLastBackup As String '最近一次备份日期和时间Public Sub ReadBackupInf()
'读配置文件中的备份信息
On Error GoTo ErrorHandle
'服务器名
G_ServerName = Readini("5678 School Management System", "ServerName", G_IniPath & "\" & G_SystemFile)
'数据库名
G_DataBaseName = Readini("5678 School Management System", "DataBaseName", G_IniPath & "\" & G_SystemFile)
'备份路径
strBackupPath = Readini("5678 School Management System", "Backuppath", G_IniPath & "\" & G_SystemFile)
'备份日期和时间
strLastBackup = Readini("5678 School Management System", "LastBackup", G_IniPath & "\" & G_SystemFile)
Exit Sub
ErrorHandle:
MsgBox "读系统配置文件出错,请检查该文件是否存在或是否有读权限!", vbCritical, "数据库备份"
End SubPrivate Sub WriteBackupInf()
'写配置文件中的备份信息
On Error GoTo ErrorHandle
'备份路径
Writeini "5678 School Management System", "Backuppath", _
strBackupPath, G_IniPath & "\" & G_SystemFile
'备份日期和时间
Writeini "5678 School Management System", "LastBackup", _
strLastBackup, G_IniPath & "\" & G_SystemFile
'备份文件名
Writeini "5678 School Management System", "LastBackupFile", _
Trim(txtBackupFile.Text), G_IniPath & "\" & G_SystemFile
Exit Sub
ErrorHandle:
MsgBox "写系统配置文件出错,请检查该文件是否存在或是否有写权限!", vbCritical, "数据库备份"
End SubPrivate Sub cmdCancel_Click()
Unload Me
End SubPrivate Sub cmdOK_Click()
Dim s As String, i As Integer
On Error GoTo ErrorBackupDatabase
If UCase(Right(txtBackupFile.Text, 4)) <> ".BAK" Then
txtBackupFile.Text = txtBackupFile.Text & ".bak"
End If
Me.MousePointer = vbHourglass
'提示信息
Frame1.Visible = True
Frame1.ZOrder 0
DoEvents
G_strSQL = "exec master..xp_cmdshell 'dir " & Trim(txtBackupPath.Text) & "'"
Set G_AdoRs = G_AdoCon.Execute(G_strSQL, , adCmdText)
If G_AdoRs.BOF Then
Frame1.Visible = False
Me.MousePointer = vbDefault
Set G_AdoRs = Nothing
MsgBox "不存在该路径!", vbExclamation, Me.Caption
Exit Sub
Else
If G_AdoRs(0).Value Like "*找不到*" Then
Frame1.Visible = False
Me.MousePointer = vbDefault
Set G_AdoRs = Nothing
MsgBox "不存在该路径!", vbExclamation, Me.Caption
Exit Sub
End If
End If
txtBackupPath.Text = Trim(txtBackupPath.Text)
If Right(txtBackupPath.Text, 1) <> "\" Then
txtBackupPath.Text = txtBackupPath.Text & "\"
End If
G_strSQL = "exec master..xp_cmdshell 'dir " & Trim(txtBackupPath.Text) & Trim(txtBackupFile.Text) & "'"
Set G_AdoRs = G_AdoCon.Execute(G_strSQL, , adCmdText)
' s = txtBackupPath.Text
' i = InStr(s, ":")
' If i <> 0 Then s = G_SchoolPath & "\" & Left(s, Len(s) - i) & Mid(s, i + 1) & Trim(txtBackupFile.Text)
' If Not (G_AdoRs(0).Value Like "*找不到*") Then
' Frame1.Visible = False
' Me.MousePointer = vbDefault
' Set G_AdoRs = Nothing
' If MsgBox("已经存在该文件,是否覆盖?", vbQuestion + vbOKCancel + vbDefaultButton2, Me.Caption) = vbCancel Then
' Exit Sub
' End If
' End If
Me.MousePointer = vbHourglass
'提示信息
Frame1.Visible = True
Frame1.ZOrder 0
DoEvents
'备份数据库
G_strSQL = "use master backup database " _
& G_DataBaseName _
& " to disk = '" & Trim(txtBackupPath.Text) _
& "\" & Trim(txtBackupFile.Text) _
& "' with init,stats,name='Full Backup of " & G_DataBaseName & "' " _
& " use " & G_DataBaseName
'执行备份数据库 G_AdoCon.Execute G_strSQL, , adCmdText
'备份路径
strBackupPath = Trim(txtBackupPath.Text)
'本次备份日期
strLastBackup = Format(Date, "yyyy/mm/dd") & Space(1) & Format(Time, "hh:mm:ss")
'保存备份信息
WriteBackupInf
Frame1.Visible = False
Me.MousePointer = vbDefault
MsgBox "备份数据操作完成!", vbInformation, "备份数据库"
Exit Sub
ErrorBackupDatabase:
Frame1.Visible = False
Me.MousePointer = vbDefault
' 将任何由执行查询引起的错误通知用户。
MsgBox "错误" & Err.Number & ":" & Err.Description, vbExclamation, "数据库备份"
End SubPrivate Sub Form_Load()
Me.Left = (Screen.Width - Me.Width) \ 1.6
Me.Top = (Screen.Height - Me.Height) * 0.5
txtBackupFile.Text = "school" & Year(Date) & Format(Month(Date), "00") & Format(Day(Date), "00") & Format(Hour(Time), "00") & Format(Minute(Time), "00") & ".bak"
ReadBackupInf '读备份信息
lblServer.Caption = G_ServerName '服务器名
lblDataBase.Caption = G_DataBaseName '数据库名称
lblLastBackup = strLastBackup '上次备份时间
txtBackupPath.Text = strBackupPath '默认的备份路径
End Sub
G_ServerName,G_DataBaseName,G_strSQL,adcmdtext,G_AdoCon,G_AdoRs,G_IniPat,G_SystemFile,Readini都是什么意思啊?
另外我要备份的数据库在哪里填写?
不胜感谢!
Option Explicit
Private strBackupPath As String '备份路径
Private strLastBackup As String '最近一次备份日期和时间Public Sub ReadBackupInf()
'读配置文件中的备份信息
On Error GoTo ErrorHandle
'服务器名
G_ServerName = Readini("5678 School Management System", "ServerName", G_IniPath & "\" & G_SystemFile)
'数据库名
G_DataBaseName = Readini("5678 School Management System", "DataBaseName", G_IniPath & "\" & G_SystemFile)
'备份路径
strBackupPath = Readini("5678 School Management System", "Backuppath", G_IniPath & "\" & G_SystemFile)
'备份日期和时间
strLastBackup = Readini("5678 School Management System", "LastBackup", G_IniPath & "\" & G_SystemFile)
Exit Sub
ErrorHandle:
MsgBox "读系统配置文件出错,请检查该文件是否存在或是否有读权限!", vbCritical, "数据库备份"
End SubPrivate Sub WriteBackupInf()
'写配置文件中的备份信息
On Error GoTo ErrorHandle
'备份路径
Writeini "5678 School Management System", "Backuppath", _
strBackupPath, G_IniPath & "\" & G_SystemFile
'备份日期和时间
Writeini "5678 School Management System", "LastBackup", _
strLastBackup, G_IniPath & "\" & G_SystemFile
'备份文件名
Writeini "5678 School Management System", "LastBackupFile", _
Trim(txtBackupFile.Text), G_IniPath & "\" & G_SystemFile
Exit Sub
ErrorHandle:
MsgBox "写系统配置文件出错,请检查该文件是否存在或是否有写权限!", vbCritical, "数据库备份"
End SubPrivate Sub cmdCancel_Click()
Unload Me
End SubPrivate Sub cmdOK_Click()
Dim s As String, i As Integer
On Error GoTo ErrorBackupDatabase
If UCase(Right(txtBackupFile.Text, 4)) <> ".BAK" Then
txtBackupFile.Text = txtBackupFile.Text & ".bak"
End If
Me.MousePointer = vbHourglass
'提示信息
Frame1.Visible = True
Frame1.ZOrder 0
DoEvents
G_strSQL = "exec master..xp_cmdshell 'dir " & Trim(txtBackupPath.Text) & "'"
Set G_AdoRs = G_AdoCon.Execute(G_strSQL, , adCmdText)
If G_AdoRs.BOF Then
Frame1.Visible = False
Me.MousePointer = vbDefault
Set G_AdoRs = Nothing
MsgBox "不存在该路径!", vbExclamation, Me.Caption
Exit Sub
Else
If G_AdoRs(0).Value Like "*找不到*" Then
Frame1.Visible = False
Me.MousePointer = vbDefault
Set G_AdoRs = Nothing
MsgBox "不存在该路径!", vbExclamation, Me.Caption
Exit Sub
End If
End If
txtBackupPath.Text = Trim(txtBackupPath.Text)
If Right(txtBackupPath.Text, 1) <> "\" Then
txtBackupPath.Text = txtBackupPath.Text & "\"
End If
G_strSQL = "exec master..xp_cmdshell 'dir " & Trim(txtBackupPath.Text) & Trim(txtBackupFile.Text) & "'"
Set G_AdoRs = G_AdoCon.Execute(G_strSQL, , adCmdText)
' s = txtBackupPath.Text
' i = InStr(s, ":")
' If i <> 0 Then s = G_SchoolPath & "\" & Left(s, Len(s) - i) & Mid(s, i + 1) & Trim(txtBackupFile.Text)
' If Not (G_AdoRs(0).Value Like "*找不到*") Then
' Frame1.Visible = False
' Me.MousePointer = vbDefault
' Set G_AdoRs = Nothing
' If MsgBox("已经存在该文件,是否覆盖?", vbQuestion + vbOKCancel + vbDefaultButton2, Me.Caption) = vbCancel Then
' Exit Sub
' End If
' End If
Me.MousePointer = vbHourglass
'提示信息
Frame1.Visible = True
Frame1.ZOrder 0
DoEvents
'备份数据库
G_strSQL = "use master backup database " _
& G_DataBaseName _
& " to disk = '" & Trim(txtBackupPath.Text) _
& "\" & Trim(txtBackupFile.Text) _
& "' with init,stats,name='Full Backup of " & G_DataBaseName & "' " _
& " use " & G_DataBaseName
'执行备份数据库 G_AdoCon.Execute G_strSQL, , adCmdText
'备份路径
strBackupPath = Trim(txtBackupPath.Text)
'本次备份日期
strLastBackup = Format(Date, "yyyy/mm/dd") & Space(1) & Format(Time, "hh:mm:ss")
'保存备份信息
WriteBackupInf
Frame1.Visible = False
Me.MousePointer = vbDefault
MsgBox "备份数据操作完成!", vbInformation, "备份数据库"
Exit Sub
ErrorBackupDatabase:
Frame1.Visible = False
Me.MousePointer = vbDefault
' 将任何由执行查询引起的错误通知用户。
MsgBox "错误" & Err.Number & ":" & Err.Description, vbExclamation, "数据库备份"
End SubPrivate Sub Form_Load()
Me.Left = (Screen.Width - Me.Width) \ 1.6
Me.Top = (Screen.Height - Me.Height) * 0.5
txtBackupFile.Text = "school" & Year(Date) & Format(Month(Date), "00") & Format(Day(Date), "00") & Format(Hour(Time), "00") & Format(Minute(Time), "00") & ".bak"
ReadBackupInf '读备份信息
lblServer.Caption = G_ServerName '服务器名
lblDataBase.Caption = G_DataBaseName '数据库名称
lblLastBackup = strLastBackup '上次备份时间
txtBackupPath.Text = strBackupPath '默认的备份路径
End Sub
G_ServerName,G_DataBaseName,G_strSQL,adcmdtext,G_AdoCon,G_AdoRs,G_IniPat,G_SystemFile,Readini都是什么意思啊?
另外我要备份的数据库在哪里填写?
不胜感谢!
另外你的备份的数据库在名字為txtBackupPath的textbox裡面,文件名字的格式
"school" & Year(Date) & Format(Month(Date), "00") & Format(Day(Date), "00") & Format(Hour(Time), "00") & Format(Minute(Time), "00") & ".bak"
為