VB中还原SQL数据库
用的SQL数据库,恢复的程序如下Form13.Caption="数据恢复中,请稍等!"Dimsql,workarea(1)AsStringSetcnn=NewADODB....
用的SQL数据库,恢复的程序如下
Form13.Caption = "数据恢复中,请稍等!"
Dim sql, workarea(1) As String
Set cnn = New ADODB.Connection
If Text1.Text <> "" Then
ProgressBar2.Max = UBound(workarea) '进度条
ProgressBar2.Value = ProgressBar2.Min
For connter = LBound(workarea) To UBound(workarea)
workarea(connter) = "initial value" & connter
ProgressBar2.Value = connter
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master"
cnn.Open
sql = "use master RESTORE DATABASE 家用理财 from disk='" & Text1.Text & "'" 'text1.text是选择还原的路径,已经正确选好了路径
cnn.Execute (sql)
cnn.Close
Next connter
ProgressBar2.Value = ProgressBar2.Max
MsgBox "数据恢复成功!", vbOKOnly, "提示信息"
Unload Me
Else
MsgBox "请选择恢复路径!", vbOKOnly, "提示信息"
End If
恢复时进度条照样显示恢复成功,完毕后,查看了一下发现并没有在我做的软件中恢复以前的数据,而且用来恢复的备份文件变为空了~ 不知道怎么回事,还请高手指点一下。谢谢了!
我是备份为.bak文件,备份的没问题,在SQL企业管理器中可以用备份的文件来还原,而且备份文件只有一个。
还是不行,不管有没有恢复成功那个文件都变为空的了。 展开
Form13.Caption = "数据恢复中,请稍等!"
Dim sql, workarea(1) As String
Set cnn = New ADODB.Connection
If Text1.Text <> "" Then
ProgressBar2.Max = UBound(workarea) '进度条
ProgressBar2.Value = ProgressBar2.Min
For connter = LBound(workarea) To UBound(workarea)
workarea(connter) = "initial value" & connter
ProgressBar2.Value = connter
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master"
cnn.Open
sql = "use master RESTORE DATABASE 家用理财 from disk='" & Text1.Text & "'" 'text1.text是选择还原的路径,已经正确选好了路径
cnn.Execute (sql)
cnn.Close
Next connter
ProgressBar2.Value = ProgressBar2.Max
MsgBox "数据恢复成功!", vbOKOnly, "提示信息"
Unload Me
Else
MsgBox "请选择恢复路径!", vbOKOnly, "提示信息"
End If
恢复时进度条照样显示恢复成功,完毕后,查看了一下发现并没有在我做的软件中恢复以前的数据,而且用来恢复的备份文件变为空了~ 不知道怎么回事,还请高手指点一下。谢谢了!
我是备份为.bak文件,备份的没问题,在SQL企业管理器中可以用备份的文件来还原,而且备份文件只有一个。
还是不行,不管有没有恢复成功那个文件都变为空的了。 展开
展开全部
我以前写过的一个代码,你参考下
酒店是我的数据库名
Public dbs As New ADODB.Connection
Public dbs1 As New ADODB.Connection ’这些放在模块里
‘以下是还原代码
If MsgBox("数据还原后需要重新启动本系统,继续吗?", 1 + 32, "提示") = vbOK Then
file = App.Path & "\酒店.bak"
If Dir(file) <> "" Then
dbs.Close
Set dbs = Nothing
dbs1.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;uid=" & yh & ";pwd=" & mm & ";" _
& "Initial Catalog=master
Dim res1 As New ADODB.Recordset
dbs1.CursorLocation = adUseClient
dbs1.Open
res1.Open "select spid from sysprocesses where dbid=db_id('酒店')", dbs1, 1, 1
Do While Not res1.EOF
dbs1.Execute "kill " & res1("spid")
res1.MoveNext
Loop
dbs1.Execute "RESTORE Database 酒店 from disk ='" & file & "' WITH REPLACE"
dbs.Open cnn
dbs1.Close
Set dbs1 = Nothing
MsgBox "数据还原成功!", 0 + 48, "提示"
End
Else
MsgBox "所要还原的文件不存在", 0 + 48, "提示"
End If
酒店是我的数据库名
Public dbs As New ADODB.Connection
Public dbs1 As New ADODB.Connection ’这些放在模块里
‘以下是还原代码
If MsgBox("数据还原后需要重新启动本系统,继续吗?", 1 + 32, "提示") = vbOK Then
file = App.Path & "\酒店.bak"
If Dir(file) <> "" Then
dbs.Close
Set dbs = Nothing
dbs1.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;uid=" & yh & ";pwd=" & mm & ";" _
& "Initial Catalog=master
Dim res1 As New ADODB.Recordset
dbs1.CursorLocation = adUseClient
dbs1.Open
res1.Open "select spid from sysprocesses where dbid=db_id('酒店')", dbs1, 1, 1
Do While Not res1.EOF
dbs1.Execute "kill " & res1("spid")
res1.MoveNext
Loop
dbs1.Execute "RESTORE Database 酒店 from disk ='" & file & "' WITH REPLACE"
dbs.Open cnn
dbs1.Close
Set dbs1 = Nothing
MsgBox "数据还原成功!", 0 + 48, "提示"
End
Else
MsgBox "所要还原的文件不存在", 0 + 48, "提示"
End If
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道你的TEXT1路径是怎么出来的,如果是用CommonDialog1打开路径的话,不要用下面的语句
Open CommonDialog1.FileName For Output As #1
Text1.Text = CommonDialog1.FileName
Close #1
要直接用
Text1.Text = CommonDialog1.FileName
不然源文件肯定会损坏
Open CommonDialog1.FileName For Output As #1
Text1.Text = CommonDialog1.FileName
Close #1
要直接用
Text1.Text = CommonDialog1.FileName
不然源文件肯定会损坏
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询