mysql 备份vb代码_VB备份与还原SQL数据库代码

Private Sub mnuDBBackUp_Click() '------------------备份数据库

Dim cnbak As New ADODB.Connection

If cnbak.State <> 0 Then cnbak.Close

cnbak.Open "provider=sqloledb;server=" & strSQLServer & ";user id=" & strSQLUser & ";password=" & strSQLPW

Dim backupSQL As String

backupSQL = "USE master " + Chr(10) + Chr(13)

backupSQL = backupSQL & "exec sp_addumpdevice 'disk','bak1'," & "'" & App.Path & "/bak1.bak" & "'"'bak1为备份设备

backupSQL = backupSQL + Chr(10) + Chr(13) + "BACKUP DATABASE SuperMarketdb TO bak1 WITH INIT"

cnbak.Execute backupSQL

MsgBox "数据库备份成功!",vbInformation

cnbak.Close

Set cnbak = Nothing

End Sub

Private Sub mnuDBResume_Click() '-------------------还原数据库

Dim cn As New ADODB.Connection,rs As New ADODB.Recordset

If Dir(App.Path & "/bak1.bak") <> vbNullString Then

If cnMain.State <> 0 Then cnMain.Close

cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & strSQLUser & ";Password=" & strSQLPW & ";Initial Catalog=master;Data Source=" & strSQLServer '借助master数据库来恢复strSQLUser是SQL登录名,Password是密码,strSQLServer是服务器名

cn.CursorLocation = adUseClient

cn.Open

rs.Open "select spid from sysprocesses where dbid=db_id('SuperMarketdb')",cn

Do While Not rs.EOF

cn.Execute "kill " & rs("spid") '杀掉sql服务器的所有连接,否则会出现:数据库正在使用,无法完成排它操作等等,很重要

rs.MoveNext

Loop

cn.Execute ("restore database SuperMarketdb from disk='" & App.Path & "/bak1.bak" & "' with replace")

cn.Close

cnMain.Open "Provider=SQLOLEDB.1;User ID=" & strSQLUser & ";Password=" & strSQLPW & ";Initial Catalog=master;Data Source=" & strSQLServer

MsgBox "数据库恢复成功!",vbInformation

Else

MsgBox "请先备份数据库!",vbInformation,"提示:"

End If

Set cn = Nothing

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值