已有22人关注
数据库备份和恢复
发表在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
这是书上原有的实例,我运行的时候提示见附件,怎么修改一下
分享到:
精彩评论 2
佚名
学分:70 LV3
TA的每日心情
开心
2021-07-05 14:21:40
2016-03-23
沙发
提示错误在数据库xy中拒绝了backupdatabase的权限
淡.印象
学分:323 LV5
TA的每日心情
学习Python
2020-11-23 22:17:24
2016-04-12
板凳
读者您好:


建议暂停SQL Server服务器试试
首页上一页 1 下一页尾页 2 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照