How to save and load a serialized Class into BLOB?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
Zero-G.
Posts: 398
Joined: Sat 09 Dec 2006 12:20

How to save and load a serialized Class into BLOB?

Post by Zero-G. » Wed 08 Aug 2007 12:59

Hey

I use VB.NET 2005 & your control v4.0
I try to save a Serialized Class into a mySQL (4.1.16) BLOB field with the following code (which seems to work)
Translated. The end of the stream was reached, before the processing was locked

Code: Select all

Dim usr As New clsBenutzer
        usr.Nachname = "Bacik"
        usr.Vorname = "Harald"
        usr.Tätigkeiten = New clsBenutzer.Benutzer() {clsBenutzer.Benutzer.Administrator, _
            clsBenutzer.Benutzer.Filialleiter}

        Dim form As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()

        ' --- Serialisieren
        Dim Stream As New System.IO.MemoryStream
        form.Serialize(Stream, usr)

        mySQLConnection.Open()
        mySQLCommand.Connection = mySQLConnection
        mySQLCommand.CommandText = "Update Mitarbeiter Set Rolle= @Data where " & _
            "ID=1"
        mySQLCommand.Parameters.AddWithValue("@Data", Stream.ToArray)
        mySQLCommand.ExecuteNonQuery()
        Stream.Close()
        mySQLConnection.Close()
        MsgBox("OK")
But the deserializing won't work - I am always getting an error message

Code: Select all

mySQLConnection.Open()
        mySQLCommand.Connection = mySQLConnection
        mySQLCommand.CommandText = "Select Rolle from Mitarbeiter where ID=1"
        Dim dAdapter As New CoreLab.MySql.MySqlDataAdapter(mySQLCommand)
        Dim dTable As New System.Data.DataTable
        dAdapter.Fill(dTable)
        Dim dRow As System.Data.DataRow = dTable.Rows(0)
        Dim b As Byte() = CType(dRow("Rolle"), Byte())

        Dim Stream As New System.IO.MemoryStream(b)
        Stream.Seek(0, IO.SeekOrigin.Begin)
        Dim usr As New clsBenutzer
        Dim form As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        ' Here I get the error
        usr = form.Deserialize(Stream)
        MsgBox("OK")
        mySQLConnection.Close()
Can please help me?

THX

Zero-G.
Posts: 398
Joined: Sat 09 Dec 2006 12:20

Post by Zero-G. » Wed 08 Aug 2007 14:08

Hey

Thanks for taking the time!
I found out, what the problem is for my self.

The tinyBlob field was to little. - THX

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Wed 08 Aug 2007 14:23

Thanks for withdrawal.

Post Reply