Binary Serialization

S

shapper

Hello,

I am trying to serialize (binary) a class and save it in a database. I
followed a few examples I found in internet and this is what I came up
with:

1 ' Rows
2 <Serializable()> _
3 Public Class Rows
4 Implements ISerializable
5
6 Private _Rows As New Generic.List(Of Row)
7 Public Property Rows() As Generic.List(Of Row)
8 Get
9 Return _Rows
10 End Get
11 Set(ByVal value As Generic.List(Of Row))
12 _Rows = value
13 End Set
14 End Property ' Rows
15
16 ' New
17 Public Sub New()
18 End Sub ' New
19
20 ' New
21 Public Sub New(ByVal siRows As SerializationInfo, ByVal scRows
As StreamingContext)
22 End Sub ' New
23
24 ' GetObjectData
25 Public Sub GetObjectData(ByVal siRows As SerializationInfo,
ByVal scRows As StreamingContext) Implements
ISerializable.GetObjectData
26
27 siRows.AddValue("Rows", Me.Rows)
28
29 End Sub ' GetObjectData
30
31 Public Sub Serialize(ByVal filename As String)
32
33 Dim sRows As Stream = Stream.Null
34 Try
35 sRows = File.Open(filename, FileMode.Create,
FileAccess.ReadWrite)
36 Dim bfRows As New BinaryFormatter
37 bfRows.Serialize(sRows, Me)
38 Finally
39 sRows.Close()
40 End Try
41
42 End Sub ' Serialize
43
44 Public Shared Function Deserialize(ByVal filename As String) As
Rows
45
46 Dim sRows As Stream = Stream.Null
47 Try
48 sRows = File.Open(filename, FileMode.Open, FileAccess.Read)
49 Dim bfRows As New BinaryFormatter
50 Return CType(bfRows.Deserialize(sRows), Rows)
51 Finally
52 sRows.Close()
53 End Try
54
55 End Function ' Deserialize
56
57 End Class ' Rows


After serializing the class I need to save it in an a SQL 2005
database.

But all the examples I followed use the filename ...

Anyway, do I need to do something to save this into an SQL 2005
database or can I use it as follows? And how can I use this?

This is the first time I do something like this so I am a little bit
confused.

Thanks,

Miguel
 
T

Teemu Keiski

You can do it by giving a object deriving from System.IO.TextWriter to the
Serialize method, instead of a file. One such is System.IO.StringWriter when
it is serialized to a string in memory. other option is to use
System.IO.MemoryStream
 
D

David Browne

shapper said:
Hello,

I am trying to serialize (binary) a class and save it in a database.
....


After serializing the class I need to save it in an a SQL 2005
database.

But all the examples I followed use the filename ...

Anyway, do I need to do something to save this into an SQL 2005
database or can I use it as follows? And how can I use this?

This is the first time I do something like this so I am a little bit
confused.

Using binary serialization to store data in a database is possible, but it's
usually considered a bad thing to do. When you do that you lock away the
data so that only your code can ever get to it, and you are bypassing all
the data management features of SQL Server. That's why you don't see a lot
of examples of doing that.

Usually you will map your object model to a relational model, storing each
object as a row in a table, with its fields broken out in columns.
Referenced objects, are stored in related tables, etc. And you use SQL
INSERT and UPDATE commands through ADO.NET to read and write data into the
database.

David
 

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,931
Messages
2,570,085
Members
46,536
Latest member
keelop

Latest Threads

Top