How to load image from SQL Server into picture box?
Asked Answered
D

4

11

I've tried a lot to find that how can I load an image from SQL Server to picture box but I couldn't find very much helpful material.

First I saved image into the data-base with the help of following query:

insert into imageTest (pic_id, pic)
values(1, 'D:\11.jpg')

Now I want to load the image into a picture box.

Deepfreeze answered 10/11, 2011 at 18:38 Comment(2)
Uh, it looks like you just "inserted the filename" -- so, 1) get the filename from SQL Server, it's just a text field (this is well-covered) 2) load the image into the picturebox using the filename (this is well-covered). However, because it's "just a filename" it also means that the file needs to accessible (with that path or something that be generated from it) on the system that wants to load it...Manufacturer
Please explain the procedure.Deepfreeze
A
31

You never uploaded the image contents to the database. That's just the file name.

Say, as an example, that you have a file path to work with (it seems you do, given the question's contents). In your application, you would upload this to the database following this format:

byte[] image = File.ReadAllBytes("D:\\11.jpg");

SqlCommand sqlCommand = new SqlCommand("INSERT INTO imageTest (pic_id, pic) VALUES (1, @Image)", yourConnectionReference);
sqlCommand.Parameters.AddWithValue("@Image", image);
sqlCommand.ExecuteNonQuery();

Please bear in mind that your pic field will more than likely need to change data type. A common type for this information is VARBINARY.

The next part is reading the file into a PictureBox. For this, you'll need to SELECT the data out:

SqlDataAdapter dataAdapter = new SqlDataAdapter(new SqlCommand("SELECT pic FROM imageTest WHERE pic_id = 1", yourConnectionReference));
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);

if (dataSet.Tables[0].Rows.Count == 1)
{
    Byte[] data = new Byte[0];
    data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]);
    MemoryStream mem = new MemoryStream(data);
    yourPictureBox.Image= Image.FromStream(mem);
} 

And that should be about it. You might want to do better safety checks, but this should help you get started.

Approach answered 10/11, 2011 at 18:41 Comment(3)
It's not necessarily a problem, depending upon overall design (although the local path is fishy). Comment-worthy: yes. Reply-worthy: not so much.Manufacturer
So, how can I upload the image? Will you please elaborate?Deepfreeze
It means you have to give me two solutions; uploading, loading the picture box. Thanks :)Deepfreeze
S
2

have a look at: http://msdn.microsoft.com/en-us/library/ms175915.aspx

your changed code:

insert into imageTest (pic_id, pic)
select 1, BulkColumn
FROM Openrowset( Bulk 'D:\11.jpg', Single_Blob) as img
Schecter answered 8/7, 2012 at 16:45 Comment(0)
M
1

See Muhammad, what we do when dealing with Images in dynamic scenarios is the following:

  1. Upload the image to some web application directory (like /images/ or something)

  2. We store the URL address in the database field of the table.

  3. Place where you want to include the image, just set the image source to the database field and it will be displayed.

Hope this helps

Maltese answered 10/11, 2011 at 19:2 Comment(0)
E
0

Check with this code:

SqlDataAdapter dAdapter = new SqlDataAdapter(new SqlCommand("SELECT Photo FROM Image", conn));
        DataSet dSet = new DataSet();
        dAdapter.Fill(dSet);

        if (dSet.Tables[0].Rows.Count == 1)
        {
            Byte[] data = new Byte[0];
            data = (Byte[])(dSet.Tables[0].Rows[0]["Photo "]);
            MemoryStream mem = new MemoryStream(data);
            PictureBoxName.Image = Image.FromStream(mem);
         }

In data mention your column name of image. Change your Picturebox name as you mention in your Form.

Estaminet answered 30/12, 2016 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.