T
Tommy Christian
Hi!
Anyone who knows about saving serialized data to database,
coz I have a problem with that. If I just serialize my session data and
then deserialize it, it works. But when I save it between those actions into
Oracle database into long row field, it doesn´t. Below is some code.
Thanks for everyone, if you know what helps.
'HERE IT SAVE´S SERIALIZED DATA TO DATABASE
Function Update()
Dim oSessionDataStream As New MemoryStream()
oSessionDataStream = Me.Serialize
'Me.Serialize is serialized data in a stream
Dim oByteArray(oSessionDataStream.Length - 1) As Byte
oSessionDataStream.Read(oByteArray, 0, oByteArray.Length)
Dim sSessionUpdateSql As String = "UPDATE TABLE SET (SDATA) = (?) WHERE
ID ='" & Me.Id & "'"
Dim oSessionDataUpdateCommand As OleDb.OleDbCommand = New
OleDbCommand(sSessionUpdateSql, Me.mWebPage.Database.Connection)
oSessionDataUpdateCommand.Parameters.Add("@SDATA",
System.Data.OleDb.OleDbType.Binary, oSessionDataStream.Length).Value =
oByteArray
oSessionDataUpdateCommand.ExecuteNonQuery()
oSessionDataStream.Close()
End Function
'HERE WE GET DATA FROM DATABASE
Public Function Retrieve() As MemoryStream
Dim sSessionRetrieveSql As OleDbCommand = New OleDbCommand("SELECT SDATA
FROM TABLE WHERE SID = '" & Me.Id & "'", mWebPage.Database.Connection)
Dim oReader As OleDbDataReader
oReader = sSessionRetrieveSql.ExecuteReader()
oReader.Read()
Dim b(oReader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
oReader.GetBytes(0, 0, b, 0, b.Length)
oReader.Close()
Dim oStream As New MemoryStream(b)
oStream.Write(b, 0, b.Length)
Return oStream
End Function
'HERE WE TRY TO DESERIALIZE DATA.
Public Function Deserialize(ByVal oStream As MemoryStream) As Session
Dim oFormatter As New BinaryFormatter()
oStream.Position = 0
Return DirectCast(oFormatter.Deserialize(oStream), Session)
End Function
'HERE IS THE ERROR WHICH COMES IN THE LAS ROW. (Return
DirectCast(oFormatter.Deserialize(oStream), Session))
System.Runtime.Serialization.SerializationException - Binary stream does not
contain a valid BinaryHeader, 0 possible causes, invalid stream or object
version change between serialization and deserialization.
'XML SERIALIZATION GIVES ERROR
root does not excist.
Anyone who knows about saving serialized data to database,
coz I have a problem with that. If I just serialize my session data and
then deserialize it, it works. But when I save it between those actions into
Oracle database into long row field, it doesn´t. Below is some code.
Thanks for everyone, if you know what helps.
'HERE IT SAVE´S SERIALIZED DATA TO DATABASE
Function Update()
Dim oSessionDataStream As New MemoryStream()
oSessionDataStream = Me.Serialize
'Me.Serialize is serialized data in a stream
Dim oByteArray(oSessionDataStream.Length - 1) As Byte
oSessionDataStream.Read(oByteArray, 0, oByteArray.Length)
Dim sSessionUpdateSql As String = "UPDATE TABLE SET (SDATA) = (?) WHERE
ID ='" & Me.Id & "'"
Dim oSessionDataUpdateCommand As OleDb.OleDbCommand = New
OleDbCommand(sSessionUpdateSql, Me.mWebPage.Database.Connection)
oSessionDataUpdateCommand.Parameters.Add("@SDATA",
System.Data.OleDb.OleDbType.Binary, oSessionDataStream.Length).Value =
oByteArray
oSessionDataUpdateCommand.ExecuteNonQuery()
oSessionDataStream.Close()
End Function
'HERE WE GET DATA FROM DATABASE
Public Function Retrieve() As MemoryStream
Dim sSessionRetrieveSql As OleDbCommand = New OleDbCommand("SELECT SDATA
FROM TABLE WHERE SID = '" & Me.Id & "'", mWebPage.Database.Connection)
Dim oReader As OleDbDataReader
oReader = sSessionRetrieveSql.ExecuteReader()
oReader.Read()
Dim b(oReader.GetBytes(0, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
oReader.GetBytes(0, 0, b, 0, b.Length)
oReader.Close()
Dim oStream As New MemoryStream(b)
oStream.Write(b, 0, b.Length)
Return oStream
End Function
'HERE WE TRY TO DESERIALIZE DATA.
Public Function Deserialize(ByVal oStream As MemoryStream) As Session
Dim oFormatter As New BinaryFormatter()
oStream.Position = 0
Return DirectCast(oFormatter.Deserialize(oStream), Session)
End Function
'HERE IS THE ERROR WHICH COMES IN THE LAS ROW. (Return
DirectCast(oFormatter.Deserialize(oStream), Session))
System.Runtime.Serialization.SerializationException - Binary stream does not
contain a valid BinaryHeader, 0 possible causes, invalid stream or object
version change between serialization and deserialization.
'XML SERIALIZATION GIVES ERROR
root does not excist.