Diana Marie

Diana Marie

  • NA
  • 1
  • 0

Storing bytes to a varbinary(MAX) field in SQL Server

Jun 11 2009 5:59 AM
Hi everyone,

I've searched countless forums for the answer to this one, and so far, I haven't found a concrete example or solution.

I'm relatively new to VB.Net, and what I'm trying to do (without success, so far) is to save a BLOB (binary large object) to a varbinary(max) field type in SQL Server 2005. The BLOB is an image, which is selected through an OpenFileDialog box. According to the tutorials that I've read, I should convert the image to bytes so that I can store it under the varbinary(MAX) field type, and this is what I've come up so far...


 fs = New FileStream(oopenpic.FileName, FileMode.OpenOrCreate, FileAccess.Read)
br = New BinaryReader(fs)
mybyte = br.ReadBytes(fs.Length)
fs.Close()

sql = "INSERT INTO tblimagetest VALUES(@thebyte)"
cmd = New SqlCommand(sql, con)
cmd.Parameters.Add("@thebyte", SqlDbType.VarBinary).Value = mybyte
cmd.ExecuteNonQuery()

I have only one column under tblimagetest, and that is the varbinary(MAX) field type image_value.

When I debugged the program, it went off without a hitch, meaning it was supposed to be saved in the table...but to my confusion and bewilderment, upon inspection of the tblimagetest table, the image_value field is empty. :( I'm completely at a loss here...was there something I missed?

Your replies will be very much appreciated.

Thank you. :)