Store images to database


This snippets is explained about, how to store the image into database.

Description



We can store the images to database in the form of bytes.

Using Memory Stream class, we can read the images as a byte format.

Here i explained the codes in windows applcation.

Following lines of code is used to save the images to memory stream.


//Initializing the memory stream.
MemoryStream tMemoryStream = new MemoryStream();

//PicbxLogo is a control name of the "PictureBox". This save method is used
to save the images to memory stream.
PicbxLogo.Image.Save(tMemoryStream, PicbxLogo.Image.RawFormat);

//Read the image as bytes.
byte[] MyData = tMemoryStream.GetBuffer();
tMemoryStream.Close();


Create the table using following query


SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Common](
[CommonID] [int] IDENTITY(1,1) NOT NULL,
[Logo] [image] NOT NULL,
CONSTRAINT [PK_Common] PRIMARY KEY CLUSTERED
(
[CommonID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]



Create a stored procedure using this query,


CREATE PROCEDURE [dbo].[SP_Common]
@AppLogo image = NULL
AS

BEGIN
INSERT INTO [dbo].[Common]
(
AppLogo
)
Select @AppLogo
END


Pass the byte values to this stored procedure as follows

con = new SqlConnection();
con.Open();
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_Common";
cmd.Parameters.Add("@AppLogo", SqlDbType.Binary, MyData.Length).Value = MyData;
cmd.ExecuteNonQuery();
con.Close();



Retrieving images from database

SqlConnection con = new SqlConnection("Data Source = DAMODHARAN-PC\\SQLEXPRESS;uid = sa; pwd = sa;database = WebSamples;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_Insert_StoreImage";
cmd.Parameters.Add("@Mode", SqlDbType.Int, 4).Value = 1;
byte[] tImageByte = cmd.ExecuteScalar() as byte[];

MemoryStream tMemoryStream = new MemoryStream(tImageByte);
PicbxLogo.Image=Image.FromStream(tMemoryStream);


Thanks & Regards,
V.M. Damodharan


Attachments

  • Store and Get Images (36447-17151-ImageStoring.rar)
  • Comments

    Author: PHANI HARSHITHA MADALA17 Feb 2010 Member Level: Gold   Points : 1

    Hi Mr.damodharan,

    Very usefull info..

    Thanks for sharing with all of us.


    regards,

    harshitha

    Author: siva17 Feb 2010 Member Level: Gold   Points : 1

    hai damodharan,
    thanks for this article,
    its very useful information.

    thanks and Regards
    siva

    Author: Ahmad Nauman10 May 2010 Member Level: Bronze   Points : 2

    There are, of course, in various positions in here, but I didn't find any post in connection with projects like hp0-m15 dump , HP0-J33... If someone has information about it, tell me! Well, all the updates related to this position? If yes, tell me! Actually I came here while surfing the network to retrieve data relating to HP0-J22 projects and found this post to a different ... Is there anyone having information on the HP0-J15?, If so, what to do, tell me! It seems to me a different type of post ...One who do not know about this before can get useful information from this post ... Well, I want to say that the way you tried to explain some of the posts here I think, different ...






  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: