473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Serialize object to database example

Does anyone have an example of serializing an object to a database table?
Nov 18 '05 #1
2 1336
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(BelgenColl ection))
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
///
Nov 18 '05 #2
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
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Ox*************@tk2msftngp13.phx.gbl...
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(BelgenColl ection))
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
///

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
5
by: Brad | last post by:
I would like to serialize an arraylist of objects to xml so I can store the xml in a database column. How would I code the serializing and deserializing? Below is a (overly) simple, incomplete...
1
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. ...
4
by: Abi | last post by:
We able to generate this error in our test environment and were able to research this enough to understand that the issue is NOT with an abject that needs to be serialized but rather as the stack...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.