Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want retrieve a picture from an Access database using C# code of visual studio 2008.
I can't find any sample code to do this. I can't show a picture in the windows form using the following code:


C#
private void load_button_Click(object sender, EventArgs e)
        {
            vcon.Open();
            OleDbDataAdapter da = new OleDbDataAdapter("select picture from dictionary where serial_no=1", vcon);  
            DataSet ds = new DataSet();
            da.Fill(ds);
            vcon.Close();
            byte[] content = (byte[])ds.Tables[0].Rows[0].ItemArray[0];
            MemoryStream stream = new MemoryStream(content);
            load_picturebox.Image = Image.FromStream(stream);

        }


Do you have any sample code that shows how to retrieve a picture in
an Access database using C# code?
Posted
Comments
Herman<T>.Instance 16-Mar-12 11:39am    
which error is given?

1 solution

read this[^].

in the given link they do:
C#
byte[] ImageByte = null;
MemoryStream MemStream = null;
PictureBox PicBx = new PictureBox();
object OB;

string WorkingDirectory = Application.StartupPath + "\\";
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + WorkingDirectory + "DBFile.mdb; Persist Security Info=True";
cnction = new OleDbConnection(connString);
cnction.Open();
int ImageID = 6;
sqlCommand = "SELECT ImageObject FROM ImagesTable WHERE ImageID = " + ImageID + "";
comm = new OleDbCommand(sqlCommand, cnction);
ImageByte = comm.ExecuteScalar();
MemStream = new MemoryStream(ImageByte);
PicBx.Image = Image.FromStream(MemStream);
}


and maybe set
C#
vcon.Close();
after
C#
load_picturebox.Image = Image.FromStream(stream);

Sometimes a closed datasource can bring you into trouble

One question :
in your source you use
C#
(byte[])ds.Tables[0].Rows[0].ItemArray[0];

Is ItemArray[0] needed ??
is
C#
(byte[])ds.Tables[0].Rows[0];
not enough?
 
Share this answer
 
v2
Comments
Member 8454009 16-Mar-12 12:52pm    
I couldn't run this code with the followed line:
ImageByte = comm.ExecuteScalar();

The error is:
"Cannot implicitly convert type 'object' to 'byte[]'. An explicit conversion exists (are you missing a cast?)"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900