呵,沙发!! 楼主,没用过ACCESS,给你SQL的数据库备份方法吧,自己琢磨一下,应该是相通的! Public Sub backup_Click(Index As Integer) Dim objSQLServer As New SQLDMO.SQLServer Dim strServer As String Dim strUserID As String Dim strPassword As String Dim strDatabase As String Dim strFile As String
Set objBackup = New SQLDMO.backup With objBackup .PercentCompleteNotification = 1 .Database = strDatabase .Files = strFile .SQLBackup objSQLServer End With Set objBackup = Nothing
objSQLServer.Close Set objSQLServer = Nothing
Screen.MousePointer = 0 Exit Sub
ErrorHandler: Screen.MousePointer = 0 MsgBox Err.Description, vbCritical End Sub Public Sub objBackup_Complete(ByVal message As String) MsgBox "备份成功!", vbOKOnly + vbExclamation, "提示" Set objBackup = Nothing End Sub
Dim fso As FileSystemObject Set fso = New FileSystemObject fso.CopyFile "C:/a.mdb","D:/a.mdb" 用这个,首先要引用microsoft script runtime
我记得有一个API函数可以在数据库打开的情况下copy这个数据库文件,以达到数据库备份!
直接用2进制方式打开文件进行读写: '备份e:\db1.mdb 为e:\db2.mdb Private Sub Command1_Click() Dim mfile As String, mfile2 As String mfile = "e:\db1.mdb" mfile2 = "e:\db2.mdb" Dim buff() As Byte Dim 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 "ok" End Sub
Public Sub backup_Click(Index As Integer)
Dim objSQLServer As New SQLDMO.SQLServer
Dim strServer As String
Dim strUserID As String
Dim strPassword As String
Dim strDatabase As String
Dim strFile As String
strServer = "192.168.1.188" '你要备份的数据库服务器地址
strUserID = "SA"
strPassword = ""
strDatabase = "storage" '数据库名称
strFile = "g:\mbd\database.bak" '你希望备份文件的放置位置
If Dir(strFile) <> "" Then
If MsgBox("文件" & strFile & "已存在,是否删除?", vbQuestion + vbYesNo) = vbYes Then
Kill strFile
Else
Exit Sub
End If
End If
Screen.MousePointer = 11
On Error GoTo ErrorHandler
objSQLServer.Connect strServer, strUserID, strPassword
Set objBackup = New SQLDMO.backup
With objBackup
.PercentCompleteNotification = 1
.Database = strDatabase
.Files = strFile
.SQLBackup objSQLServer
End With
Set objBackup = Nothing
objSQLServer.Close
Set objSQLServer = Nothing
Screen.MousePointer = 0
Exit Sub
ErrorHandler:
Screen.MousePointer = 0
MsgBox Err.Description, vbCritical
End Sub
Public Sub objBackup_Complete(ByVal message As String)
MsgBox "备份成功!", vbOKOnly + vbExclamation, "提示"
Set objBackup = Nothing
End Sub
Set fso = New FileSystemObject
fso.CopyFile "C:/a.mdb","D:/a.mdb"
用这个,首先要引用microsoft script runtime
'备份e:\db1.mdb 为e:\db2.mdb
Private Sub Command1_Click()
Dim mfile As String, mfile2 As String
mfile = "e:\db1.mdb"
mfile2 = "e:\db2.mdb"
Dim buff() As Byte
Dim 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 "ok"
End Sub
http://searchdatabase.techtarget.com.cn/tips/174/2054674.shtml