百瑞赢证券:用vb写的一段恢复sql的代码,有问题,请教

来源:百度文库 编辑:高考问答 时间:2024/04/29 08:27:50
Private Sub restore_Click()

Dim bakfile As String
CommonDialog1.Filter = "备份文件(*.bak)|*.bak"
CommonDialog1.ShowOpen
bakfile = CommonDialog1.FileName

If bakfile = "" Or Dir(bakfile) = "" Then
MsgBox "文件不存在,请重新选择。", vbExclamation
Exit Sub
Else
On Error GoTo error_proc
con.Close
Set con = New ADODB.Connection
con.ConnectionString = "Provider=SQLOLEDB;data source=127.0.0.1;initial catalog=master;user id=sa"
con.CursorLocation = adUseClient
con.CommandTimeout = 500
con.Open
Dim s2 As String
s2 = "restore database 学生选课系统 from disk='" & bakfile &"

Dim com1 As ADODB.Command
Set com1 = New ADODB.Command

com1.ActiveConnection = con
com1.CommandText = s2
com1.CommandType = adCmdText

com1.Execute
MsgBox "数据还原完毕,请按“确定”返回", vbInformation, "完毕"
Exit Sub
End If
error_proc:
MsgBox "还原数据库时出现错误!" & vbNewLine & Err.Description, vbExclamation, "错误"

End Sub

报错:因为数据库正在使用,所以未能获得对数据库的排它访问权。

要怎么改了~谢谢
那应该怎么改呢

二次连续访问数据库:
第一次:
Set con = New ADODB.Connection
con.ConnectionString = "Provider=SQLOLEDB;data source=127.0.0.1;initial catalog=master;user id=sa"

第二次:
Dim com1 As ADODB.Command
Set com1 = New ADODB.Command

com1.ActiveConnection = con
com1.CommandText = s2
com1.CommandType = adCmdText

是不可以的