- 相關推薦
用VB實現(xiàn)對庫文件的分割備份
用VB實現(xiàn)對庫文件的分割備份
殷鵬
在設計信息管理程序時,數(shù)據(jù)庫的軟盤備份與恢復是我們經(jīng)常遇到的一個問題。如果僅僅使用文件復制指令,那么在文件大小超過軟盤容量時,就會出現(xiàn)錯誤。下面我來介紹一種用VB實現(xiàn)的對文件進行分割備份的方法。假設在E:\CLASS目錄下有一個名為CLASSES.MDB的文件,3.8M,我們現(xiàn)在要按照每個1.4M,把它分割成3個文件進行備份,然后,再恢復成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:"恢復"。
分別雙擊按鈕,填入以下代碼:
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ù)結構
SplitFileName = "e:\class\classes.mdb"
FilesLen = FileLen(SplitFileName)
fNum = FreeFile
Open SplitFileName For Binary As fNum '計算備份參數(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 '相關數(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 '恢復后文件名
Dim fNum As Integer '可用的文件號
Dim tmpFileName As String '臨時文件名
Dim File As SectionedFile '參數(shù)結構
Dim FileInfo As FileInfo '臨時文件的結構
tmpFileName = "e:\class\classes.mdb.tpl"
OutName = "e:\class\classes1.mdb"
> fNum = FreeFile
Open tmpFileName For Binary As #fNum '取得相關數(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 '恢復
For i = 1 To FileInfo.FileCount
Put #fNum, , File.Files(i).Bytes
Next
Close #fNum
End Sub
運行該例程,點擊“備份”按鈕,用資源管理器看看E:\CLASS目錄,可以看到分割后的三個文件:CLASSES.MDB.000、CLASSES.MDB.001、CLASSES.MDB.002,大小分別為:1.33M、1.33M、1.1M,還有一個存放參數(shù)的文件:CLASSES.MDB.TPL,大。1K。點擊“恢復”按鈕,這些文件又被恢復成了CLASSES1.MDB。
以上程序在PWIN98、VB6環(huán)境下運行通過。需要的朋友可以加入拷貝到A盤等語句,這樣,一個實用的備份與恢復程序就完成了。
作者:殷鵬
郵編:235000
地址:安徽省淮北市教育巷A棟706號
Email: yinpeng@21cn.com
【用VB實現(xiàn)對庫文件的分割備份】相關文章:
用Windows終端仿真程序實現(xiàn)微機遠程文件傳送08-06
用Windows終端仿真程序實現(xiàn)微機遠程文件傳送108-06
VB訪問數(shù)據(jù)庫的方法及接口的比較08-06
VB開發(fā)客戶服務器軟件的設計與實現(xiàn)08-06
用VB構鍵Internet的應用08-06
用VB制作網(wǎng)址鏈接器08-06
用VB構鍵Internet的應用-08-06