Forum Moderators: open
I need to create a function in .Net to store and retrieve session variable from SQL server. I have datatable I put in session variable and I would like to save it to the database. I know how to do with strings but do not know how to do it with datatable object.
I do not want to create separate sql table it has to be stored in one field.
Can I do it with out using serialization?
thanks for help.
'''''
Imports System.Runtime.Serialization.Formatters.Binary
'''''
Public Function serializeDatatable(ByVal dt As DataTable) As Byte()
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
objMemoryStream = New MemoryStream
objBinaryFormatter = New BinaryFormatter
objBinaryFormatter.Serialize(objMemoryStream, dt)
objMemoryStream.Close()
Return objMemoryStream.ToArray
End Function
Public Function deserializeDatatable(ByVal arrByte As Byte()) As DataTable
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
objMemoryStream = New MemoryStream(arrByte)
objBinaryFormatter = New BinaryFormatter
Dim dt As DataTable = CType(objBinaryFormatter.Deserialize(objMemoryStream), DataTable)
objMemoryStream.Close()
Return dt
End Function