首页上一页 1 下一页尾页 2 条记录 1/1页
数据库备份和恢复
发表在VB答疑区
2016-03-23
是否精华
是
否
版块置顶:
是
否
Dim StrCnn As New Connection '定义连接
Dim itmX As ListItem '定义一个ListItem对象
Dim key, list, sql As String '定义字符串变量
Private Sub Form_Load()
Option1.Value = True '设置Option1的值为真
Dir1_Change
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
'添加数据备份卡到列表中
ListView1.ListItems.Clear
If File1.ListCount <> 0 Then
a = 0 '给变量a设置初值
Do While File1.ListIndex < File1.ListCount - 1
File1.ListIndex = a '赋值给变量a
key = File1.FileName
Set itmX = ListView1.ListItems.Add(, , key, 1)
a = a + 1
Loop
End If
File1_Click
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
Label1.Caption = Dir1.Path & "\" & File1.FileName '获得路径
End Sub
Private Sub ListView1_Click()
Label1.Caption = Dir1.Path & "\" & ListView1.SelectedItem '获得路径
End Sub
Private Sub CmdBackup_Click()
Dim S
If Option1.Value = True Then
'备份数据库
S = Dir1.Path & "\" & Date & ".BAK"
If Label1.Caption = S Then
MsgBox "数据备份卡已存在!", , "提示窗口"
Else
StrCnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa"
'备份SQL Server数据库
sql = "backup DATABASE db_EMS TO disk='" & S & "'"
StrCnn.Execute (sql) '执行SQL语句
StrCnn.Close
MsgBox "数据库备份成功!", , "提示窗口"
key = Date & ".BAK"
Set itmX = ListView1.ListItems.Add(, , key, 1)
End If
End If
If Option2.Value = True Then
'恢复指定路径下的数据库
If File1.ListCount <> 0 Then
If Label1.Caption <> "" Then
StrCnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=" & Winsock1.LocalHostName
'数据恢复语句
sql = "RESTORE DATABASE db_EMS from disk='" & Label1.Caption & "'"
StrCnn.Execute (sql) '执行SQL语句
StrCnn.Close
MsgBox "数据库恢复成功!", , "提示窗口"
Else
MsgBox "请选择要恢复的数据备份卡!", , "提示窗口"
End If
Else
MsgBox "请选择要恢复的数据备份卡!", , "提示窗口"
End If
End If
End Sub
Private Sub CmdEnd_Click()
End
End Sub
这是书上原有的实例,我运行的时候提示见附件,怎么修改一下
Dim itmX As ListItem '定义一个ListItem对象
Dim key, list, sql As String '定义字符串变量
Private Sub Form_Load()
Option1.Value = True '设置Option1的值为真
Dir1_Change
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
'添加数据备份卡到列表中
ListView1.ListItems.Clear
If File1.ListCount <> 0 Then
a = 0 '给变量a设置初值
Do While File1.ListIndex < File1.ListCount - 1
File1.ListIndex = a '赋值给变量a
key = File1.FileName
Set itmX = ListView1.ListItems.Add(, , key, 1)
a = a + 1
Loop
End If
File1_Click
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
Label1.Caption = Dir1.Path & "\" & File1.FileName '获得路径
End Sub
Private Sub ListView1_Click()
Label1.Caption = Dir1.Path & "\" & ListView1.SelectedItem '获得路径
End Sub
Private Sub CmdBackup_Click()
Dim S
If Option1.Value = True Then
'备份数据库
S = Dir1.Path & "\" & Date & ".BAK"
If Label1.Caption = S Then
MsgBox "数据备份卡已存在!", , "提示窗口"
Else
StrCnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa"
'备份SQL Server数据库
sql = "backup DATABASE db_EMS TO disk='" & S & "'"
StrCnn.Execute (sql) '执行SQL语句
StrCnn.Close
MsgBox "数据库备份成功!", , "提示窗口"
key = Date & ".BAK"
Set itmX = ListView1.ListItems.Add(, , key, 1)
End If
End If
If Option2.Value = True Then
'恢复指定路径下的数据库
If File1.ListCount <> 0 Then
If Label1.Caption <> "" Then
StrCnn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=" & Winsock1.LocalHostName
'数据恢复语句
sql = "RESTORE DATABASE db_EMS from disk='" & Label1.Caption & "'"
StrCnn.Execute (sql) '执行SQL语句
StrCnn.Close
MsgBox "数据库恢复成功!", , "提示窗口"
Else
MsgBox "请选择要恢复的数据备份卡!", , "提示窗口"
End If
Else
MsgBox "请选择要恢复的数据备份卡!", , "提示窗口"
End If
End If
End Sub
Private Sub CmdEnd_Click()
End
End Sub
这是书上原有的实例,我运行的时候提示见附件,怎么修改一下