KnowDotNet Visual Organizer

Binary Serialization of a DataSet - ADO.NET 2.0

by William Ryan
Print this Article Discuss in Forums

There's just so much cool new stuff in the new framework to really go through it all.  I'm still just fumbling around the ADO.NET libraries and playing with some of the goodies.  One cool feature is the Binary Serialization support in addition to XML Serialization.  One caution though, it's not exactly as intuitive as you might think.  When you hear that there's binary serialization, you may assume that it's simply a matter of calling a WriteBinary or ReadBinary method.  Actually, you need to set the .RemotingFormat property of type SerializationFormat (and call the Binary enumeration).  Anyway, it's probably easier to just show you an example:

First, a little groundwork so  I can easily fill a DataSet:

Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Xml

Next, fill it:


Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
        
Dim cs As String = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind"
        
Dim sql As String = "SELECT * FROM Employees"
        
Dim cn As New SqlConnection(cs)
        
Dim cmd As New SqlCommand(sql, cn)
        
Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "TestTable")
        
Me.WriteBinary("C:\testbinary.bin", ds)
        ds.WriteXml("C:\testbinary.xml")

End Sub

Public
Sub WriteBinary(ByVal FileName As String, ByVal ds As DataSet)
        ds.RemotingFormat = SerializationFormat.Binary
        
Dim myFormatter As IFormatter = New Formatters.Binary.BinaryFormatter
        
Dim myStream As Stream = New FileStream(FileName, FileMode.Create)
        myFormatter.Serialize(myStream, ds)
End Sub

Public
Function ReadBinary(ByVal FileName As String) As DataSet
        
Dim ds As New DataSet
        ds.RemotingFormat = SerializationFormat.Binary
        
Dim myFormatter As IFormatter = New Formatters.Binary.BinaryFormatter
        
Dim myStream As Stream = New FileStream(FileName, FileMode.Open)
        ds =
CType(myFormatter.Deserialize(myStream), DataSet)
        
Return ds
End Function


When I compare the files, testbinary.bin is 231kb whereas its XML Counterpart [testbinary.xml] is 263kb.  At first this doesn't look like a big deal, but the differences grow as the sizes of the file increase - this particular table has only 7 records so it's pretty thin on the data side.

Next, deserialize everything:


Private Sub btnDeserialize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeserialize.Click
        
Dim ds As New DataSet
        ds =
Me.ReadBinary("C:\testbinary.bin")
        Debug.Assert(ds.Tables.Count > 1, "Ain't go no data")
        Debug.Assert(ds.Tables(0).Rows.Count > 1, "Ain't got no rows")
End Sub

Well, that's about it to get simple serialization to work.  Obviously it has a slightly different implementation (and easier one) to get it to work in a Remoting or Web Service context - should have that up shortly ;-)  Also,
Angel Saenz-Badillo has been a huge help in getting me up to speed with the new ADO.NET 2.0 functionality.  Check out his blog for updates.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing