-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks