0

问题 :

我需要将数据表以二进制格式保存到二进制文件中,以便加快处理速度,因为数据表可能包含多达一千万行。因此,XML 不受欢迎,因为它会使文件变大,并且处理过程会很慢。我设法将数据表保存到二进制文件中,它工作正常,但是当我尝试向现有二进制文件添加新行时出现问题(使用具有相同架构但不同行数据的数据表),它复制了架构将数据表转换为二进制文件,使其非常大。

需要什么:

我需要能够只向现有的二进制文件添加行。如果您尝试下面的代码并运行 3 次,它应该会创建二进制文件,然后每次保存添加 5 行,即保存 15 行。但这种情况并非如此。它将保存表模式和 5 行(二进制格式)>>> 然后表模式和 5 行(二进制格式)>>>表模式和 5 行(二进制格式)。表架构本身非常大,并且消耗很多文件大小。我只需要保存一次文件架构,然后是 15 行。

我的代码:函数 GetTable() As DataTable

Dim table As New DataTable ' Create new DataTable instance.

table.Columns.Add("Dosage", GetType(Integer)) ' Create four typed columns in the DataTable.
table.Columns.Add("Drug", GetType(String))
table.Columns.Add("Patient", GetType(String))
table.Columns.Add("Date", GetType(DateTime))
' Add five rows with those columns filled in the DataTable.
table.Rows.Add(25, "Indocin", "David", DateTime.Now)
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
Return table
End Function

Private Sub SaveDataTabletoBinary()
dt = GetTable()

Dim format As New Binary.BinaryFormatter
Dim ds As New DataSet
' ds = DataGridView1.DataSource

Using fs As New FileStream("c:\sar1.txt", FileMode.Append)
dt.RemotingFormat = SerializationFormat.Binary

'Other option is SerilaizationFormat.XML 
format.Serialize(fs, ds)
End Using
End Sub

任何想法。?

谢谢

4

1 回答 1

0

尝试使用扩展名为的二进制文件.bin

于 2012-05-26T15:05:27.517 回答