By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

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

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. Function GetTable() As DataTable
  3.         Dim table As New DataTable     ' Create new DataTable instance.
  5.         table.Columns.Add("Dosage", GetType(Integer))  ' Create four typed columns in the DataTable.
  6.         table.Columns.Add("Drug", GetType(String))
  7.         table.Columns.Add("Patient", GetType(String))
  8.         table.Columns.Add("Date", GetType(DateTime))
  9.         ' Add five rows with those columns filled in the DataTable.
  10.         table.Rows.Add(25, "Indocin", "David", DateTime.Now)
  11.         table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
  12.         table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
  13.         table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
  14.         table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
  15.         Return table
  16.     End Function
  18.      Private Sub SaveDataTabletoBinary()
  19.         dt = GetTable()
  21.         Dim format As New Binary.BinaryFormatter
  22.         Dim ds As New DataSet
  23.         ' ds = DataGridView1.DataSource
  25.         Using fs As New FileStream("c:\sar1.txt", FileMode.Append)
  26.             dt.RemotingFormat = SerializationFormat.Binary
  28.             'Other option is SerilaizationFormat.XML 
  29.             format.Serialize(fs, ds)
  30.         End Using
  31.     End Sub
Apr 29 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.