-3
有誰知道如何直接從dBase .DBF/.DBT文件集中讀取數據?如何直接讀取dbf/dbt文件?
詳情:
我試圖根據規格的dBase的DBF/DBT文件寫一個解析器。
DBF文件相對簡單,MEMO字段中的值是該字段的數據應該始於dbt文件中的順序塊編號。
DBT文件在規範中沒有深入定義。當我解析DBT文件時(根據規範由連續塊組成[每個大小爲512字節],塊0是頭塊),我看到散佈在記錄數據之間的額外字節數據(有些看起來像「垃圾」二進制數據,有些看起來像數據庫中的其他表名)。對於一些包含字母/數字的額外數據,它試圖只讀取幾乎不可能的塊的記錄數據。在我能看到的規格中沒有對這些奇怪數據的清晰定義。我假設它可能是某種標題數據,但它似乎沒有固定的字節寬度,甚至不出現在每個塊的相同位置。
此外,DBF文件的備註字段中的順序塊編號並不總是與實際數據對齊。即dbf中的記錄2表示它從塊2開始,但實際上在dbt文件中從塊6開始。
有沒有人知道關於DBT文件結構的更多信息?也許我失蹤的東西?
代碼示例(VB.Net):
' Holds information about data in the header .dbf file.
Public Class HeaderFileClass
Public Property AccountNo As String ' 6 bytes
Public Property BlockNumber As String '10 bytes
Public Property DateInfo As String '8 bytes
Public Property EditBy As String '3 bytes
Public Sub New()
AccountNo = String.Empty
BlockNumber = String.Empty
DateInfo = String.Empty
EditBy = String.Empty
End Sub
Public Sub New(newAcctNo As String, newBlockNo As String, newDateInf As String, newEditBy As String)
AccountNo = newAcctNo
BlockNumber = newBlockNo
DateInfo = newDateInf
EditBy = newEditBy
End Sub
End Class
' Strips a byte array of anything but alpha-numerics, space, or line feed.
Private Function CleanBytes(ByRef bytes As Byte()) As Byte()
Dim newBytes As Byte()
Dim BLOCKSIZE As Integer = 512
Dim j As Integer = 0
Dim strOut As String = String.Empty
ReDim newBytes(BLOCKSIZE)
newBytes.Initialize()
For Each i As Byte In bytes
Dim intVal As Integer = Convert.ToInt32(i)
If (intVal >= 32 And intVal <= 126) Or intVal = 10 Then
newBytes(j) = i
j += 1
End If
Next
Return newBytes
End Function
Private Sub ParseFile()
Dim fileName As String = "C:\dbbackup\Schalls\Schalls_CleanLegacy\Schall_Clean_DATA\PATNOTES" ' data location.
Dim BLOCKSIZE As Integer = 512 ' Default block size.
Dim bytes As Byte() = Nothing ' bytes to be read from dbt file.
Dim buffer As Char() ' buffer to use for reading dbf file.
Dim hList As New List(Of HeaderFileClass) ' DBF header data storage.
Dim lstData As New List(Of Byte()) ' DBT block data storage.
ReDim buffer(28) ' Set size of buffer array.
'header file load
Using inFile As New StreamReader(File.Open(fileName & ".DBF", FileMode.Open))
' read DBF header lines.
inFile.ReadLine()
inFile.ReadLine()
' read DBF data.
While inFile.Read(buffer, 0, 28) > 0
Dim strBuf As New String(buffer)
Dim acctNo As String = strBuf.Substring(0, 7)
Dim blockNo As String = strBuf.Substring(7, 10).Trim
Dim dateInfo As String = strBuf.Substring(17, 8)
Dim editBy As String = strBuf.Substring(25, 3)
hList.Add(New HeaderFileClass(acctNo, blockNo, dateInfo, editBy))
End While
End Using
'memo file load
Using inFile As New BinaryReader(File.Open(fileName & ".DBT", FileMode.Open))
' read data sequentially by blocksize.
Do
bytes = inFile.ReadBytes(BLOCKSIZE)
If bytes.Length > 0 Then
lstData.Add(bytes)
End If
Loop While bytes.Length > 0
End Using
If hList.Count > 2 Then
For i As Integer = 0 To hList.Count - 2
Dim h As HeaderFileClass = hList(i) ' get data for the current record from the header file data. (contains block number to start)
Dim h2 As HeaderFileClass = hList(i + 1) ' get the next data for the current record. (contains next starting block number)
Dim intFrom As Integer = CInt(h.BlockNumber) ' starting block number.
Dim intTo As Integer = CInt(h2.BlockNumber) ' next record's starting block number.
Dim sbStr As New System.Text.StringBuilder ' output string.
' read the bytes, ensure they are text data,
For j As Integer = intFrom To intTo - 1
sbStr.Append(System.Text.Encoding.ASCII.GetString(CleanBytes(lstData(j))))
Next
Debug.Print(sbStr.ToString)
Next
End If
End Sub
你有沒有編寫任何代碼?請將相關的代碼發佈到問題中,或者懷疑您有問題。 –
我會在大約30分鐘內到達工作計算機後立即發佈代碼。 –
我希望你在發帖之前閱讀過這個問題:http://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm – i486