1 Replies - 9527 Views - Last Post: 30 April 2012 - 01:24 PM Rate Topic: -----

#1 waleednosir   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 10

serialize datatable to a binary file , then add new rows to file

Posted 29 April 2012 - 03:58 PM

Dear Experts,

Problem :

I need to save a datatable to a binary file , in binary format , in order to make the process fast because the datatable may contain up ten millions rows. So , XML is not favorable because it makes the file large sized , and the process will be slow.
I managed to save the datatable to a binary file , and it works fine , but the problem when I try to add new rows to the existing binary file (using a datatable with the same schema , but different rows data) , it copies the schema of the datatable to the binary file , making it very large.

What is needed:

I need to be able to only add rows to the existing binary file. If you try the below code and run it 3 times , it should create the binary file and then add 5 rows for each save , ie , save 15 rows . But this is not the case. it will save table schema & 5 rows (in binary format) >>> then table schema & 5 rows (in binary format) >>>table schema & 5 rows (in binary format). Table schema itself is very large , and consumes much file size. I need to save the file schema only once , then the 15 rows.
My Code:


Function 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

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: serialize datatable to a binary file , then add new rows to file

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: serialize datatable to a binary file , then add new rows to file

Posted 30 April 2012 - 01:24 PM

Is there a question in there somewhere? - Please read this, then adjust your post and title.

:code:

Personally I think you need to use a database and not a file. The very nature of files is that you have to keep re-writing old data to insert new data. We have an entire section of tutorials devoted to database use.

This post has been edited by tlhIn`toq: 30 April 2012 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1