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

store and retrieve image to/from sql server database

By , 5 Aug 2007
 

Introduction

hi.I have a database for save large image and binary data. we use a simple method for save and retrieve image from sqlserver database.

Background

this article useful for every application programmer in related work.

we use this method in LG-iomind company

Using the code

At first we have two main storedprocedure for save and retrieve image

CREATE PROCEDURE sr_fetch_image(@ID int) AS
BEGIN
    SELECT    [Name], Exc, Image_Data
    FROM         [dbimage].[dbo].[tbl_Image]
    WHERE [ID]=@ID
END
GO

CREATE PROCEDURE [sr_insert_Image]
    (
     @Name     [nvarchar](50),
     @Exc     [nvarchar](50),
     @Image_Data     [image])

AS INSERT INTO [dbimage].[dbo].[tbl_Image] 
     (
     [Name],
     [Exc],
     [Image_Data]) 
 
VALUES 
    ( 
     @Name,
     @Exc,
     @Image_Data)
GO

and some code in C# for save and fetching

      // Save image in database
      OpenFileDialog odlg = new OpenFileDialog();
      odlg.ShowDialog();
      Cursor.Current = Cursors.WaitCursor;
      this.Refresh();
      Image bmp=Bitmap.FromFile(odlg.FileName);
      System.IO.MemoryStream ms=new System.IO.MemoryStream();
      bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
      sr_fetch_imageTableAdapter.Insert("df", "rer", ms.GetBuffer());
      Cursor.Current = Cursors.Default;
 
      // fetch data from database
      sr_fetch_imageTableAdapter.Fill(dSMoblie.sr_fetch_image, 3);
      System.IO.MemoryStream ms = new System.IO.MemoryStream();
      //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
      ms.Write(dSMoblie.sr_fetch_image[0].Image_Data, 0, dSMoblie.sr_fetch_image[0].Image_Data.Length);
      Image bmp = Bitmap.FromStream(ms);
      pictureBox1.Image = bmp;

note that sr_fetch_imageTabelAdapter is my table adapter that have to stored procedure for save and fetching

License

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

jalal khodabandeh
Web Developer
Greece Greece
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGood Example try itmembermayurmv5 Sep '08 - 17:24 
Visit http://developmentzone.blogspot.com/2008/09/store-and-retrieve-image-in-sql-server.html[^]
Questionhow to call sql server 2005 cursors in vc++ by using ADOmemberram on c#11 Nov '07 - 22:40 
Hi friends,
 
what is the approach to call cursors are prepared in SQL server in vc++6.0 by using ADO
 
thanks,
Rambabu.
GeneralQuestionmemberthund3rstruck6 Aug '07 - 2:03 
Do the .NET data controls handle images in a database? So for example is it possible to bind a template row in a gridview, datalist, or repeater directly to images in the database? I have always stored the path to the images in the database but I can see the value in storing the actual image in the database as oppossed to on the filesystem.

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 6 Aug 2007
Article Copyright 2007 by jalal khodabandeh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid