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 oSessionDataStr eam As New MemoryStream()
oSessionDataStr eam = Me.Serialize
'Me.Serialize is serialized data in a stream
Dim oByteArray(oSes sionDataStream. Length - 1) As Byte
oSessionDataStr eam.Read(oByteA rray, 0, oByteArray.Leng th)
Dim sSessionUpdateS ql As String = "UPDATE TABLE SET (SDATA) = (?) WHERE
ID ='" & Me.Id & "'"
Dim oSessionDataUpd ateCommand As OleDb.OleDbComm and = New
OleDbCommand(sS essionUpdateSql , Me.mWebPage.Dat abase.Connectio n)
oSessionDataUpd ateCommand.Para meters.Add("@SD ATA",
System.Data.Ole Db.OleDbType.Bi nary, oSessionDataStr eam.Length).Val ue =
oByteArray
oSessionDataUpd ateCommand.Exec uteNonQuery()
oSessionDataStr eam.Close()
End Function
'HERE WE GET DATA FROM DATABASE
Public Function Retrieve() As MemoryStream
Dim sSessionRetriev eSql As OleDbCommand = New OleDbCommand("S ELECT SDATA
FROM TABLE WHERE SID = '" & Me.Id & "'", mWebPage.Databa se.Connection)
Dim oReader As OleDbDataReader
oReader = sSessionRetriev eSql.ExecuteRea der()
oReader.Read()
Dim b(oReader.GetBy tes(0, 0, Nothing, 0, Integer.MaxValu e) - 1) As Byte
oReader.GetByte s(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(ByV al oStream As MemoryStream) As Session
Dim oFormatter As New BinaryFormatter ()
oStream.Positio n = 0
Return DirectCast(oFor matter.Deserial ize(oStream), Session)
End Function
'HERE IS THE ERROR WHICH COMES IN THE LAS ROW. (Return
DirectCast(oFor matter.Deserial ize(oStream), Session))
System.Runtime. Serialization.S erializationExc eption - 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 oSessionDataStr eam As New MemoryStream()
oSessionDataStr eam = Me.Serialize
'Me.Serialize is serialized data in a stream
Dim oByteArray(oSes sionDataStream. Length - 1) As Byte
oSessionDataStr eam.Read(oByteA rray, 0, oByteArray.Leng th)
Dim sSessionUpdateS ql As String = "UPDATE TABLE SET (SDATA) = (?) WHERE
ID ='" & Me.Id & "'"
Dim oSessionDataUpd ateCommand As OleDb.OleDbComm and = New
OleDbCommand(sS essionUpdateSql , Me.mWebPage.Dat abase.Connectio n)
oSessionDataUpd ateCommand.Para meters.Add("@SD ATA",
System.Data.Ole Db.OleDbType.Bi nary, oSessionDataStr eam.Length).Val ue =
oByteArray
oSessionDataUpd ateCommand.Exec uteNonQuery()
oSessionDataStr eam.Close()
End Function
'HERE WE GET DATA FROM DATABASE
Public Function Retrieve() As MemoryStream
Dim sSessionRetriev eSql As OleDbCommand = New OleDbCommand("S ELECT SDATA
FROM TABLE WHERE SID = '" & Me.Id & "'", mWebPage.Databa se.Connection)
Dim oReader As OleDbDataReader
oReader = sSessionRetriev eSql.ExecuteRea der()
oReader.Read()
Dim b(oReader.GetBy tes(0, 0, Nothing, 0, Integer.MaxValu e) - 1) As Byte
oReader.GetByte s(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(ByV al oStream As MemoryStream) As Session
Dim oFormatter As New BinaryFormatter ()
oStream.Positio n = 0
Return DirectCast(oFor matter.Deserial ize(oStream), Session)
End Function
'HERE IS THE ERROR WHICH COMES IN THE LAS ROW. (Return
DirectCast(oFor matter.Deserial ize(oStream), Session))
System.Runtime. Serialization.S erializationExc eption - 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.