Serialize object to database example

C

Cor Ligthert

Hi Marcel,

I made this sample saterday morning after some arguing in the language.vb
group
It is complete most is to prove it works.

The sample is windowforms, so that have you to change it yourself a little
bit, however is serialize and it deserialize.

I hope it was what you where looking for

Cor

\\\
Private WithEvents combobox1 As New ComboBox
Private strt As Boolean
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim Belgen As New BelgenCollection
Belgen.Add(New Belg(New String() {"Lambiek", "Man", "Jenever"}))
Belgen.Add(New Belg(New String() {"Sidonia", "Vrouw", "Likeur"}))
Belgen.Add(New Belg(New String() {"Suske", "Jongen", "Cola"}))
Belgen.Add(New Belg(New String() {"Wiske", "Meisje", "Limonade"}))
Belgen.Add(New Belg(New String() {"Jerom", "Jongeman", "Bier"}))
Dim Serializer As New
Xml.Serialization.XmlSerializer(GetType(BelgenCollection))
Dim sw As New System.IO.StringWriter
Serializer.Serialize(sw, Belgen)
Dim ds As New DataSet
Dim dt As New DataTable("Jay")
Dim dc As New DataColumn("Charles")
'This part only for Jay and Charles if he wants
dt.Columns.Add(dc)
ds.Tables.Add(dt)
dt.Rows.Add(dt.NewRow)
ds.Tables(0).Rows(0)(0) = sw.ToString
ds.WriteXml("c:\test1\charles.xml")
Dim dsJay As New DataSet
dsJay.ReadXml("c:\test1\Charles.xml")
Dim Charles As String = dsJay.Tables(0).Rows(0)(0).ToString
'End Jay part
Dim Deserializer As New Xml.Serialization.XmlSerializer _
(GetType(BelgenCollection))
Dim sr As New System.IO.StringReader(Charles)
Dim reader As New System.Xml.XmlTextReader(sr)
Dim Vlamingen As BelgenCollection
Vlamingen = CType(Deserializer.Deserialize(reader), _
BelgenCollection)
Me.Controls.Add(combobox1)
combobox1.DataSource = Vlamingen
strt = True
End Sub
Private Sub combobox1_SelectedIndexChanged(ByVal _
sender As Object, ByVal e As System.EventArgs) _
Handles combobox1.SelectedIndexChanged
If strt = True Then
MessageBox.Show(DirectCast(combobox1.SelectedValue, Belg).drank)
End If
End Sub
End Class
///
\\\
Public Class Belg
Public Naam As String
Public sex As String
Public drank As String
Public Sub New()
End Sub
Public Sub New(ByVal fill As String())
Naam = fill(0)
sex = fill(1)
drank = fill(2)
End Sub
Public Overrides Function tostring() As String
Return Naam
End Function
End Class
Public Class BelgenCollection
Inherits System.Collections.CollectionBase
Default Public Overloads ReadOnly _
Property Item(ByVal index As Integer) As Belg
Get
Return DirectCast(list.Item(index), Belg)
End Get
End Property
Public Sub Add(ByVal aBelg As Belg)
List.Add(aBelg)
End Sub
End Class
///
 
M

Marcel Balcarek

Thank you Cor,
I was planning to use the BinaryFormatter class. I would then store the
object in binary format in a varbinary column in a general table.
Is this a common practice? Any further tips?
Marcel
I
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top