用vb写的一段恢复sql的代码,有问题,请教
PrivateSubrestore_Click()DimbakfileAsStringCommonDialog1.Filter="备份文件(*.bak)|*.bak"Co...
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
报错:因为数据库正在使用,所以未能获得对数据库的排它访问权。
要怎么改了~谢谢
那应该怎么改呢 展开
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
报错:因为数据库正在使用,所以未能获得对数据库的排它访问权。
要怎么改了~谢谢
那应该怎么改呢 展开
1个回答
展开全部
二次连续访问则悄数据库:
第一次肢早:
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
是不可以的
第一次肢早:
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
是不可以的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询