Binary Serialization

Discussion in 'ASP .Net' started by shapper, Mar 9, 2007.

  1. shapper

    shapper Guest

    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
     
    shapper, Mar 9, 2007
    #1
    1. Advertising

  2. shapper

    Teemu Keiski Guest

    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

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "shapper" <> wrote in message
    news:...
    > 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
    >
     
    Teemu Keiski, Mar 10, 2007
    #2
    1. Advertising

  3. shapper

    David Browne Guest

    "shapper" <> wrote in message
    news:...
    > 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
     
    David Browne, Mar 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex D.

    binary vs. xml serialization

    Alex D., Dec 21, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    9,346
    Alex D.
    Dec 21, 2005
  2. Replies:
    8
    Views:
    2,253
    deadsea
    Jan 2, 2005
  3. Replies:
    3
    Views:
    1,037
  4. Dimitri Ognibene
    Replies:
    4
    Views:
    785
    Dimitri Ognibene
    Sep 2, 2006
  5. Ramunas Urbonas
    Replies:
    1
    Views:
    403
    Dino Chiesa [Microsoft]
    Jul 27, 2004
Loading...

Share This Page