How to retrieve an image and display from a table in Microsoft Access DB?

In a table of Microsoft Access DB, images are stored in an object-type column. Now I want to retrieve them out and display in C#, how to do Had better give me some sample codes if possible.

Answer this question

How to retrieve an image and display from a table in Microsoft Access DB?

  • Byron118766

    Well even in this case it can be done in a pretty simiar fashion.

    I am referencing you an artilcle in MSDN which does the same thing using nortwind database

    http://msdn2.microsoft.com/library/y0h25we8(en-us,vs.80).aspx

    There is another aricle at c-sharp corner which might help  you in the work

    http://www.c-sharpcorner.com/winforms/ImageViewerST.asp

    Hopefully these two articles above will help and you can always fine tune the applications according to your needs Big Smile

    Best's

    AfterBurner
    MCP


  • ACSM99

    Hi, AfterBurner,

    Your codes is to display images on Web page, but my purpose is to retrieve & display images on a pictureBox of Windows Form. Do you have any idea

    In addition, do you know whether OLE objects stored in Access DB can exist independent from those image files imported from I mean if those image files are removed, can those OLE objects still be available

    Thanks for the help.

    Big Smile

  • AlexJD

    Here you go!

    using System.Data;
    using System.Data.OleDb;


    The System.Data.OleDb namespace contains the components necessary to implement the OLE DB data provider.

    OleDbConnection Myconnection = null;
    OleDbDataReader dbReader = null;

    Myconnection = new OleDbConnection (@"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; Password=; Data Source=C:\MyDatabase.mdb");
    Myconnection.Open();

    OleDbCommand cmd = Myconnection.CreateCommand();
    cmd.CommandText = "SELECT * FROM ImageTable"; //adjust the where clause according to the ID
    dbReader = cmd.ExecuteReader();

    My ImageTable has the following schema

    ImageTable

    [img_id] is a seed (Auto Number)
    [img_name] which is a text
    [img_data] OLE Object
    [img_contenttype] Text

    You will be using image_id to retrive the particular image from the database.

    if ( dr.Read()) //yup we found our image
    {
    Response.ContentType = dr["img_contenttype"].ToString();
    Response.BinaryWrite( (byte[]) dr["img_data"] );
    }

    connection.Close();

    As simple as a buffalow :)

    AfterBurner
    MCP




  • Al Grant

    AfterBurner,

    The problem is that I don't know how to convert an Image object to a byte array (byte []). It seems that Convert.ChangeType() method does not support any conversion to byte []. Please help me to overcome the final obstacle.

    moonriver

  • dalangalma

    This will solve the conversion issue :)
    byte[] byteArray = Convert.FromBase64String( base64String );
    MemoryStream memStrm = new MemoryStream( byteArray, 0, byteArray.Length );
    retObj = Image.FromStream( memStrm );
                                        
    memStrm.Close();

  • rusold

    AfterBurner,

    You mean that I have to convert the Img Object to a string, and then convert the string to a byte[] subsequently. However, it seems unworkable. When I attempted to convert the string to a byte array, an exception was triggered that there are unreadable characters in the string.

    Moonriver

  • How to retrieve an image and display from a table in Microsoft Access DB?