2012-04-30 20 views
0

問題:連載數據表二進制文件,然後添加新行到文件

我需要一個數據表保存到一個二進制文件,二進制格式,爲了使這一進程快,因爲數據表最多可包含一千萬行。所以,XML是不利的,因爲它會使文件變大,並且過程會很慢。 我設法將數據表保存到一個二進制文件,並且它工作正常,但是當我嘗試向現有二進制文件添加新行時(使用具有相同模式但不同行數據的數據表)時,它會複製數據表的架構到二進制文件,使其非常大。

需要什麼樣的:

我需要能夠只行添加到現有的二進制文件。如果您嘗試下面的代碼並運行它3次,它應該創建二進制文件,然後爲每次保存添加5行,即保存15行。但這種情況並非如此。它會保存表格模式& 5行(二進制格式)>>>然後表格模式& 5行(二進制格式)>>>表格模式& 5行(二進制格式)。表格模式本身非常大,並且消耗很多文件大小。我只需要保存一次文件模式,然後保存15行。

我的代碼: 功能GetTable()作爲數據表

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 

的想法。 ?

感謝

回答

0

嘗試使用其中有.bin

擴展名的二進制文件
相關問題