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