vb中实现access数据库备份和数据库恢复问题
vb中实现access数据库备份和数据库恢复,具体就是窗体中有个按纽,直接单击就实现备份数据库功能,并且是弹出对话框,自由选择路径保存数据库。
数据库恢复也是一样。
谢谢大家帮忙,急切等待大家的支持,我是个新手,谢谢请教!
问题点数:20、回复次数:12Top
1 楼DengXingJie(杰西)回复于 2006-06-23 16:37:25 得分 0
ACCESS的備份:就是把MDB文件COPY到其它的位置
ACCESS的還原:就是把MDB從其它位置COPY回來
Top
2 楼leongwong()回复于 2006-06-23 16:38:12 得分 5
1.添加声明
Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
2.备份
Dim mOldFile As String
Dim mNewFile As String
Dim mTemp As String
mTemp = Year(Now) & Month(Now) & Day(Now) & Int(10 * Rnd) '& Time()
mOldFile = App.Path & "\xxxx.mdb"
mNewFile = App.Path & "\backup\xxxx" & mTemp & ".mdb"
CopyFile mOldFile, mNewFile, Not True
MsgBox 备份完成"
Exit Sub
3.恢复
Dim mOldFile As String
Dim mNewFile As String
dlgDBop.InitDir = App.Path
dlgDBop.Filter = "备份文件(*.mdb)|*.mdb|所有文件(*.*)|*.*|"
dlgDBop.ShowOpen
mOldFile = dlgDBop.FileName
If mOldFile = "" Then Exit Sub
mNewFile = App.Path & "\xxxx.mdb"
CopyFile mOldFile, mNewFile, Not True
MsgBox "恢复完成,请重新启动程序"
Exit Sub
Top
3 楼leongwong()回复于 2006-06-23 16:40:38 得分 0
再直接一点的!
备份:
直接拷贝数据库文件就行
FileCopy "c:\db.mdb", "c:\dbbak.mdb"
恢复的时候:
要求程序里不能访问数据库,将文件拷贝回来
Top
4 楼guiyuanlianzi()回复于 2006-06-23 16:41:42 得分 0
还要备份的路径是自由选择的,而且保存的文件名也要自己随便起的啊Top
5 楼leongwong()回复于 2006-06-23 16:42:23 得分 0
代码:
Dim fso As New FileSystemObject
'strSourceFile 源文件
'strDestinationFile 目标文件
fso.CopyFile strSourceFile, strDestinationFile, False
'最后一个参数为是否覆盖
FileCopy strSourceFile, strDestinationFileTop
6 楼faysky2(出来混,迟早是要还嘀)回复于 2006-06-23 16:43:53 得分 0
这个刚好合适:
http://community.csdn.net/Expert/TopicView3.asp?id=4608048Top
7 楼guiyuanlianzi()回复于 2006-06-23 17:17:46 得分 0
数据库恢复 能帮我写吗?小妹刚学,还没入门,多谢啦!Top
8 楼faysky2(出来混,迟早是要还嘀)回复于 2006-06-23 17:26:11 得分 15
添加CommonDialog控件到窗体上(工程/部件/Microsoft/CommonDialog Control 6.0)
备份当前文件夹下的 db1.mdb 到别的地方去:
Private Sub Command1_Click()
Dim mfile As String, mfile2 As String
On Error Resume Next
CommonDialog1.Filter = "Access文件(*.mdb)|*.mdb"
CommonDialog1.ShowSave
mfile = App.Path & "\db1.mdb" '要备份的文件为当前文件夹下的 db1.mdb
mfile2 = CommonDialog1.FileName '得到目标文件的路径
If Trim(mfile2) = "" Then Exit Sub
If Dir(mfile2) <> "" Then
If MsgBox(Dir(mfile2) & " 文件已经存在,是否替换?", vbYesNo, "警告") = vbNo Then Exit Sub
End If
Dim buff() As Byte, i As Long
i = FileLen(mfile)
ReDim buff(i - 1)
Open mfile For Binary As #1
Get #1, , buff
Close #1
Open mfile2 For Binary As #1
Put #1, , buff
Close #1
MsgBox "备份完毕!"
End SubTop
9 楼faysky2(出来混,迟早是要还嘀)回复于 2006-06-23 17:32:59 得分 0
恢复文件是一样的原理,就是用别的地方的Access文件覆盖掉当前目录下的Access文件:
Private Sub Command2_Click()
Dim mfile As String, mfile2 As String
On Error Resume Next
CommonDialog1.Filter = "Access文件(*.mdb)|*.mdb"
CommonDialog1.ShowOpen
mfile = CommonDialog1.FileName '得到别处的Access文件的路径
mfile2 = App.Path & "\db1.mdb" '要覆盖掉当前文件夹下的 db1.mdb
If Trim(mfile) = "" Then Exit Sub
If MsgBox("是否恢复数据库?", vbYesNo, "警告") = vbNo Then Exit Sub
Dim buff() As Byte, i As Long
i = FileLen(mfile)
ReDim buff(i - 1)
Open mfile For Binary As #1
Get #1, , buff
Close #1
Open mfile2 For Binary As #1
Put #1, , buff
Close #1
MsgBox "恢复完毕!"
End Sub
Top
10 楼guiyuanlianzi()回复于 2006-06-24 09:23:48 得分 0
十分感谢Top
11 楼leungzhq2000(月下独茁)回复于 2006-10-15 09:26:40 得分 0
学到很多东西,在此同样表示感谢!
Top
12 楼linlitong()回复于 2007-01-21 11:24:13 得分 0
App.Path是什么意思啊Top




