Click here to Skip to main content
14,738,362 members
Articles » Database » Database » General
Posted 17 Jan 2005


104 bookmarked

Load/Unload images into/from DB table

Rate me:
Please Sign up or sign in to vote.
3.88/5 (26 votes)
24 Aug 2005
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 Pin
RedDk29-Sep-11 9:15
MemberRedDk29-Sep-11 9:15 
GeneralMy vote of 4 Pin
Amir Mehrabi-Jorshari23-Jul-10 3:27
MemberAmir Mehrabi-Jorshari23-Jul-10 3:27 
GeneralСпасибо! Pin
skiner13-Oct-08 12:51
Memberskiner13-Oct-08 12:51 
GeneralSave an Image to SQL2000 server using OLEDB connection Pin
mahalirajesh13-Mar-07 18:13
Membermahalirajesh13-Mar-07 18:13 
GeneralHelp reagarding inserting Image in Oracle Databse using OLEDB Pin
rupangupta28-Jul-06 4:07
Memberrupangupta28-Jul-06 4:07 
GeneralHelp reagarding inserting Image in Oracle Databse using OLEDB Pin
rupangupta28-Jul-06 3:49
Memberrupangupta28-Jul-06 3:49 
GeneralThe Code for Pin
nazrulislam21-Jul-06 3:15
Membernazrulislam21-Jul-06 3:15 
GeneralRe: The Code for Pin
Maxim Alekseykin21-Jul-06 7:09
MemberMaxim Alekseykin21-Jul-06 7:09 
GeneralRe: The Code for Pin
torabis874-Aug-09 5:24
Membertorabis874-Aug-09 5:24 
QuestionError Saving the Image Pin
mig166-Mar-06 11:23
Membermig166-Mar-06 11:23 
AnswerRe: Error Saving the Image Pin
mig166-Mar-06 11:37
Membermig166-Mar-06 11:37 
GeneralRe: Error Saving the Image Pin
maziarm29-May-07 11:08
Membermaziarm29-May-07 11:08 
GeneralSHow image .net Pin
Hanieef27-Feb-06 21:44
MemberHanieef27-Feb-06 21:44 
GeneralOne problem... Pin
Antonio Dias31-Aug-05 11:06
MemberAntonio Dias31-Aug-05 11:06 
GeneralRe: One problem... Pin
Lev Vayner.5-Oct-07 9:51
professionalLev Vayner.5-Oct-07 9:51 
Questionwhy does it has error when running? Pin
uumeme12-Aug-05 4:03
Memberuumeme12-Aug-05 4:03 
AnswerRe: why does it has error when running? Pin
Anonymous12-Aug-05 5:08
MemberAnonymous12-Aug-05 5:08 
GeneralRe: why does it has error when running? Pin
uumeme16-Aug-05 20:05
Memberuumeme16-Aug-05 20:05 
GeneralRe: why does it has error when running? Pin
Maxim Alekseykin18-Aug-05 5:35
MemberMaxim Alekseykin18-Aug-05 5:35 
GeneralThank! Pin
JaimirG20-Jun-05 6:48
MemberJaimirG20-Jun-05 6:48 
GeneralLoad Pin
rm_babar28-Apr-05 21:40
Memberrm_babar28-Apr-05 21:40 
GeneralError Pin
lyptus23-Feb-05 12:53
Memberlyptus23-Feb-05 12:53 
GeneralRe: Error Pin
lyptus23-Feb-05 12:57
Memberlyptus23-Feb-05 12:57 
GeneralRe: Error Pin
Maxim Alekseykin24-Feb-05 5:00
MemberMaxim Alekseykin24-Feb-05 5:00 
GeneralGood simple artcile Pin
Nirosh9-Feb-05 17:41
professionalNirosh9-Feb-05 17:41 

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

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