Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I Store an Object in SQL Server?

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
GB
Hi

I have a object variable in my DALC which could contain a string, int, class, image or any other datatype.

What I need to do is store this object in SQL Server. I am using an Image data type in SQL Server to store this data but have no idea how to convert my object into a format that can be sent into my stored procedure which expects an Image data type.

Can anyone tell me how to do this.

TIA

Smeat
 
Hi VBakias

I understand how to use serialization but I have no idea how to convert my object into a format that can be used to send into my stored procedure which expects an Image data type.

Any help appreciated

Smeat
 
// create a byte[] for the image file that is uploaded
int imagelen = Upload.PostedFile.ContentLength;
byte[] picbyte = new byte[imagelen];
Upload.PostedFile.InputStream.Read (picbyte, 0, imagelen);
// Insert the image and image id into the database
SqlConnection conn = new SqlConnection
// (" connection string ...");
try
{
conn.Open ();
SqlCommand cmd = new SqlCommand ("insert into ImageTable " + "(ImageField, ImageID) values (@pic, @imageid)", conn);
cmd.Parameters.Add ("@pic", picbyte);
cmd.Parameters.Add ("@imageid", "image text");
cmd.ExecuteNonQuery ();
}
finally
{
conn.Close ();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top