Click here to Skip to main content
Click here to Skip to main content

Load/Unload images into/from DB table

, 24 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Explains how to load a BLOB data into a DB table and how to get it from the DB table.

Sample Image - imageStorage.jpg


We all often need to store Binary Large Objects (BLOBs) into a DB table and then get them from there. Now I'm going to explain the easiest way to do this.

Prepare Database

Run SQL Server Enterprise Manager and create a new database, call it 'Test'. Create a new table and call it Images.

CREATE TABLE Images ([stream] [image] NULL)

That's all you need.

Store Image into DB table

byte[] content = ReadBitmap2ByteArray(fileName);
protected static byte[] ReadBitmap2ByteArray(string fileName)
  using(Bitmap image = new Bitmap(fileName))
    MemoryStream stream = new MemoryStream();
    image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
    return stream.ToArray();

protected static void StoreBlob2DataBase(byte[] content)
   SqlConnection con = Connection;
     // insert new entry into table
     SqlCommand insert = new SqlCommand(
     "insert into Images ([stream]) values (@image)",con);
     SqlParameter imageParameter = 
     insert.Parameters.Add("@image", SqlDbType.Binary);
     imageParameter.Value = content;
     imageParameter.Size  = content.Length;

Store Images for OLEDB provider

Some of us use OLEDB provider to communicate with SQL Server. In this case you should use the code below to store images into your DB. Pay attention to using '?' instead of '@image' in the SQL query.

protected static void StoreBlob2DataBaseOleDb(byte[] content)
      using(OleDbConnection con = Connection)

         // insert new entry into table
         using(OleDbCommand insert = new OleDbCommand(
             "insert into Images ([stream]) values (?)",con))
            OleDbParameter imageParameter = 
            insert.Parameters.Add("@image", OleDbType.Binary);
            imageParameter.Value = content;
            imageParameter.Size  = content.Length;
   catch(Exception ex)
      // some exception processing

Get Image from DB table and show it

 // get image
 DataRowView drv = (DataRowView) _cm.Current;
 byte[] content = (byte[])drv["stream"];
 MemoryStream stream = new MemoryStream(content);
 Bitmap image = new Bitmap(stream);
 ShowImageForm f = new ShowImageForm();
 f._viewer.Image = image;


You can use this technique to work with any type of binary data without using storage procedures. Good Luck.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Maxim Alekseykin
Team Leader
Russian Federation Russian Federation
Now is looking for remote job.
- C++/C#, VB/VBA, SQL Server/Access databases.
- automatic testing, code review
- performance tuning

Comments and Discussions

QuestionThis simple solution to stuffing image data in an SQL database compiles @ VS2010 PinmemberRedDK29-Sep-11 9:15 
GeneralMy vote of 4 PinmemberAmir Mehrabi-Jorshary23-Jul-10 3:27 
GeneralСпасибо! Pinmemberskiner13-Oct-08 12:51 
GeneralExample with SQL Server Pinmembermayurmv5-Sep-08 18:21 
GeneralSave an Image to SQL2000 server using OLEDB connection Pinmembermahalirajesh13-Mar-07 18:13 
GeneralHelp reagarding inserting Image in Oracle Databse using OLEDB Pinmemberrupangupta28-Jul-06 4:07 
GeneralHelp reagarding inserting Image in Oracle Databse using OLEDB Pinmemberrupangupta28-Jul-06 3:49 
GeneralThe Code for Pinmembernazrulislam21-Jul-06 3:15 
GeneralRe: The Code for PinmemberMaxim Alekseikin21-Jul-06 7:09 
GeneralRe: The Code for Pinmembertorabis874-Aug-09 5:24 
QuestionError Saving the Image Pinmembermig166-Mar-06 11:23 
AnswerRe: Error Saving the Image Pinmembermig166-Mar-06 11:37 
GeneralRe: Error Saving the Image Pinmembermaziarm29-May-07 11:08 
GeneralSHow image .net PinmemberHanieef27-Feb-06 21:44 
GeneralOne problem... PinsussAntonio Dias31-Aug-05 11:06 
GeneralRe: One problem... PinmemberLev Vayner.5-Oct-07 9:51 
Questionwhy does it has error when running? Pinmemberuumeme12-Aug-05 4:03 
AnswerRe: why does it has error when running? PinsussAnonymous12-Aug-05 5:08 
GeneralRe: why does it has error when running? Pinmemberuumeme16-Aug-05 20:05 
GeneralRe: why does it has error when running? PinmemberMaxim Alekseikin18-Aug-05 5:35 
GeneralThank! PinmemberJaimirG20-Jun-05 6:48 
GeneralLoad Pinmemberrm_babar28-Apr-05 21:40 
GeneralError Pinmemberlyptus23-Feb-05 12:53 
GeneralRe: Error Pinmemberlyptus23-Feb-05 12:57 
GeneralRe: Error PinmemberMaxim Alekseikin, MCAD24-Feb-05 5:00 
GeneralGood simple artcile PinmemberNirosh9-Feb-05 17:41 
GeneralRe: Good simple artcile PinmemberMaxim Alekseikin, MCAD22-Feb-05 1:18 
GeneralRe: Good simple artcile PinmemberNirosh1-Nov-06 3:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150129.1 | Last Updated 24 Aug 2005
Article Copyright 2005 by Maxim Alekseykin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid