3

I have a table that have a field as Varbinary(MAX) datatype and I want to insert image file from PictureBox into this column. So how can I convert and insert image to Varbinary in Sql Server. Thank for help me.

1 Answer 1

2

You should really show some code, what have you tried...

This is only a guess, but it should give you a clue how to insert picture into database:

//byte array that will hold image data
byte[] imageData = null;

using (var ms = new MemoryStream())
{
    //here is image property of your pictureBox control  saved into memory stream
    pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    imageData = ms.ToArray();
}

//make sql connection
SqlConnection conn = new SqlConnection("your connection string goes here");
// command with parameter
SqlCommand cmd = new SqlCommand("insert into TableWithImages (imageData) values (@imageData);", conn);
//define param and pass byte array as value
cmd.Parameters.Add("@imageData", SqlDbType.VarBinary).Value = imageData;

//do insert
cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.