- 相關(guān)推薦
用VB實(shí)現(xiàn)對庫文件的分割備份
用VB實(shí)現(xiàn)對庫文件的分割備份
殷鵬
在設(shè)計(jì)信息管理程序時,數(shù)據(jù)庫的軟盤備份與恢復(fù)是我們經(jīng)常遇到的一個問題。如果僅僅使用文件復(fù)制指令,那么在文件大小超過軟盤容量時,就會出現(xiàn)錯誤。下面我來介紹一種用VB實(shí)現(xiàn)的對文件進(jìn)行分割備份的方法。假設(shè)在E:\CLASS目錄下有一個名為CLASSES.MDB的文件,3.8M,我們現(xiàn)在要按照每個1.4M,把它分割成3個文件進(jìn)行備份,然后,再恢復(fù)成CLASSES1.MDB文件。啟動VB,新建一個工程,添加一個模塊,將下列代碼填入模塊的聲明中:
Type FileSection
Bytes() As Byte
FileLen As Long
End Type
Type SectionedFile
Files() As FileSection
NumberOfFiles As Long
End Type
Type FileInfo
OrigProjSize As Long
OrigFileName As String
FileCount As Integer
FileStartNum As Long
End Type
然后,在Form1中添加兩個按鈕,分別命名為cmdBackup,Caption:"備份";cmdRestore,Caption:"恢復(fù)"。
分別雙擊按鈕,填入以下代碼:
Private Sub cmdBackup_Click()
Dim SplitFileName As String '待備份的文件名
Dim Split As Long '備份文件的大小
Dim SaveName As String '備份文件名
Dim fNum As Integer '可用的文件號
Dim NumFil As Long '備份文件的數(shù)目
Dim FilesLen As Long '待備份文件的大小
Dim CurrentFile As SectionedFile '參數(shù)結(jié)構(gòu)
SplitFileName = "e:\class\classes.mdb"
FilesLen = FileLen(SplitFileName)
fNum = FreeFile
Open SplitFileName For Binary As fNum '計(jì)算備份參數(shù)
NumFil = CInt(FilesLen /
ReDim CurrentFile.Files(1 To NumFil)
For i = 1 To NumFil
 
; ReDim CurrentFile.Files(i).Bytes(1 To
CurrentFile.Files(i).FileLen = UBound(CurrentFile.Files(i).Bytes)
Next
For i = 1 To NumFil
Get #fNum, , CurrentFile.Files(i).Bytes
Next
ReDim CurrentFile.Files(NumFil).Bytes(1 To FilesLen - ((NumFil - 1) *
CurrentFile.NumberOfFiles = NumFil
Get #fNum, , CurrentFile.Files(NumFil).Bytes
CurrentFile.Files(NumFil).FileLen = UBound(CurrentFile.Files(NumFil).Bytes)
Close #fNum
For i = 1 To CurrentFile.NumberOfFiles '分割備份
SaveName = SplitFileName & "." & Format(BeginningNumber - 1 + i, "00#")
fNum = FreeFile
Open SaveName For Binary As fNum
Put #fNum, 1, CurrentFile.Files(i)
Close #fNum
Next
Dim FileInfoFile As FileInfo '相關(guān)數(shù)據(jù)寫入臨時文件
FileInfoFile.FileCount = NumFil
FileInfoFile.OrigFileName = SplitFileName
FileInfoFile.OrigProjSize = FileLen(SplitFileName)
FileInfoFile.FileStartNum = BeginningNumber
SaveName = SplitFileName & ".tpl"
fNum = FreeFile
Open SaveName For Binary As #fNum
Put #fNum, , FileInfoFile
Close #fNum
End Sub
Private Sub cmdRestore_Click()
Dim OutName As String '恢復(fù)后文件名
Dim fNum As Integer '可用的文件號
Dim tmpFileName As String '臨時文件名
Dim File As SectionedFile '參數(shù)結(jié)構(gòu)
Dim FileInfo As FileInfo '臨時文件的結(jié)構(gòu)
tmpFileName = "e:\class\classes.mdb.tpl"
OutName = "e:\class\classes1.mdb"
> fNum = FreeFile
Open tmpFileName For Binary As #fNum '取得相關(guān)數(shù)據(jù)
Get #fNum, , FileInfo
Close #fNum
ReDim File.Files(1 To FileInfo.FileCount) '打開備份文件
For i = 1 To FileInfo.FileCount
OpenName = FileInfo.OrigFileName & "." & Format((FileInfo.FileStartNum - 1 + i), "00#")
fNum = FreeFile
Open OpenName For Binary As #fNum
Get #fNum, 1, File.Files(i)
Close #fNum
Next
fNum = FreeFile
Open OutName For Binary As #fNum '恢復(fù)
For i = 1 To FileInfo.FileCount
Put #fNum, , File.Files(i).Bytes
Next
Close #fNum
End Sub
運(yùn)行該例程,點(diǎn)擊“備份”按鈕,用資源管理器看看E:\CLASS目錄,可以看到分割后的三個文件:CLASSES.MDB.000、CLASSES.MDB.001、CLASSES.MDB.002,大小分別為:1.33M、1.33M、1.1M,還有一個存放參數(shù)的文件:CLASSES.MDB.TPL,大。1K。點(diǎn)擊“恢復(fù)”按鈕,這些文件又被恢復(fù)成了CLASSES1.MDB。
以上程序在PWIN98、VB6環(huán)境下運(yùn)行通過。需要的朋友可以加入拷貝到A盤等語句,這樣,一個實(shí)用的備份與恢復(fù)程序就完成了。
作者:殷鵬
郵編:235000
地址:安徽省淮北市教育巷A棟706號
Email: yinpeng@21cn.com
【用VB實(shí)現(xiàn)對庫文件的分割備份】相關(guān)文章:
用Windows終端仿真程序?qū)崿F(xiàn)微機(jī)遠(yuǎn)程文件傳送08-06
用Windows終端仿真程序?qū)崿F(xiàn)微機(jī)遠(yuǎn)程文件傳送108-06
sql數(shù)據(jù)庫備份和恢復(fù)常用操作08-12
VB訪問數(shù)據(jù)庫的方法及接口的比較08-06
VB開發(fā)客戶服務(wù)器軟件的設(shè)計(jì)與實(shí)現(xiàn)08-06
利用VB實(shí)現(xiàn)PC機(jī)與多單片機(jī)通訊08-06
用VB構(gòu)鍵Internet的應(yīng)用08-06
用VB制作網(wǎng)址鏈接器08-06