Storing a vb class object in a data base?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Storing a vb class object in a data base?

  1. #1
    lm Guest

    Storing a vb class object in a data base?

    It is possible to store an entire class object in a database field (long
    binary?). I'm looking for a way to store all private and public data, I
    guess like a "retreivable memory snapshot" or something. It would work
    something like this:

    Dim objOriginal as new clsObject
    Dim objNew as Object
    Dim Index as Integer

    SaveToDataBase Index, objOriginal
    Set objOriginal = Nothing

    Set objNew = ReadFromDataBase(Index)

    objNew should now have the same contents as objOriginal had.
    Does enybody have any suggestions or ideas how something similar could be
    accomplished.

    Thanks
    Lars B.



  2. #2
    Eric Litwin Guest

    Re: Storing a vb class object in a data base?


    "lm" <xwx@ix.netcom.com> wrote:
    >It is possible to store an entire class object in a database field (long
    >binary?). I'm looking for a way to store all private and public data, I
    >guess like a "retreivable memory snapshot" or something. It would work
    >something like this:
    >
    >Dim objOriginal as new clsObject
    >Dim objNew as Object
    >Dim Index as Integer
    >
    >SaveToDataBase Index, objOriginal
    >Set objOriginal = Nothing
    >
    >Set objNew = ReadFromDataBase(Index)
    >
    >objNew should now have the same contents as objOriginal had.
    >Does enybody have any suggestions or ideas how something similar could be
    >accomplished.
    >
    >Thanks
    >Lars B.
    >
    >


    You need to serialize your object state when you save it to the database
    and de-serialize it when you retrieve it.

    Serializing/de-serializing is a complex subject and there are many ways you
    could approach it.

    The easiest way would be to use a PropertyBag object and write each member
    variable (property) in your object to the PropertyBag. You can then serialize
    the PropertyBag to a byte array (using the Contents function) and save that
    byte array as a string to your database.

    You could then read that string back from the database, set it as the contents
    of a new PropertyBag object, and then read each Property and assign it to
    your member variables.

    For example - a simple Employee object:

    Option Explicit

    Private mlngId As Long
    Private mstrName As String
    Private mcurSalary As Currency

    ' Temporary database - just use a local string for demo purposes
    Private mDatabase As String

    Public Sub WriteToDatabase()
    Dim propBag As New PropertyBag
    Dim Buffer As String

    propBag.WriteProperty "Name", mstrName
    propBag.WriteProperty "Salary", mcurSalary

    Buffer = propBag.Contents

    ' Code to save the Buffer to database goes here
    mDatabase = Buffer
    End Sub

    Public Sub ReadFromDatabase(Id As Long)
    Dim propBag As New PropertyBag
    Dim strBuffer As String
    Dim arBuffer() As Byte

    ' Read from Database using Id and assign to strBuffer
    strBuffer = mDatabase

    ' Read the string into a byte array
    arBuffer = strBuffer

    propBag.Contents = arBuffer

    ' Set our properties
    mlngId = Id
    mstrName = propBag.ReadProperty("Name", "")
    mcurSalary = propBag.ReadProperty("Salary", 0)
    End Sub


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center