Saving a class instance to a database - HELP !?!

S

Ste

Hi group,
having done some searching on Google i have managed to save an
instance of my class to a database by first serializing it to a file.

The question i have is: can i do this without having to use a temporary
file ?

Here is the code i'm using:

'Serialize class to filestream
Dim formatter As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim stream As New System.IO.FileStream("Input.bin",
System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None)
formatter.Serialize(stream, usrPallets)

'Declare a byte array to save the content of the file to be saved
Dim FileByteArray(stream.Length - 1) As Byte
stream.Read(FileByteArray, 0, stream.Length)

'Add parameter to odbc command
cmdTest.Parameters.Clear()
cmdTest.Parameters.Add(New Microsoft.Data.Odbc.OdbcParameter("Test",
Microsoft.Data.Odbc.OdbcType.Binary, stream.Length,
System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte),
"PALLET", System.Data.DataRowVersion.Current, ""))
cmdTest.Parameters(0).Value = FileByteArray
stream.Close()

'Run SQL insert statement
conTESTSS.Open()
cmdTest.ExecuteNonQuery()
conTESTSS.Close()


TIA,

Ste..
 
A

Armin Zingler

Ste said:
Hi group,
having done some searching on Google i have managed to
save an
instance of my class to a database by first serializing it to a
file.

The question i have is: can i do this without having to use a
temporary file ?

Use a System.IO.MemoryStream instead of a Filestream (without having
evaluated the process itself).
 
H

Herfried K. Wagner [MVP]

* "Ste said:
having done some searching on Google i have managed to save an
instance of my class to a database by first serializing it to a file.

The question i have is: can i do this without having to use a temporary
file ?

Serialize it to a 'MemoryStream' (part of 'System.IO').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top