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 |
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 |
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 |