inserting images in varbinary datatype
Can somebody tell me how to store image in varbinary data though SQL Server or do I have to write code in ASP .net to read the image file and store in database
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
ManuelPosted Jul 31, 2007, 5:35 AM
You should define that column with type Image. Now you can do it like this:
System.IO.MemoryStream memStream = new System.IO.MemoryStream( );// save image to a memory stream
Image.FromFile( fileName ).Save( memStream, System.Drawing.Imaging.ImageFormat.Bmp ); // create SqlCommand and add a parameter
SqlCommand sqlCmd = new SqlCommand( "INSERT INTO TableName ( ImageColumn ) VALUES ( @Image )", sqlConnection );
sqlCmd.Parameters.Add( "@Image", SqlDbType.Image ); // set image as byte[] to parameter
sqlCmd.Parameters [ "@Image" ].Value = memStream.GetBuffer( ); // execute command
sqlCmd.ExecuteNonQuery( );