Inserting the picture into the database

C

Comfort

I have tried the following way and fail!! I was using VB.NET. Is that the
reason? How can I get the file control and how can I convert the file
control into the server object. Below is the code I used:



Introduction

This sample helps you to store and retrieve Images in any format into MS SQL
Server Database tables through ASP.NET Application. Create a table with 3
fields with a field as 'Image' datatype. Add new asp.net project and add
new web form. Add a File control and name it as 'File1' and make it as
Server Control. Add a Submit button.

Press F7 to view the webpage Code-behind and add the following code. The
code given here is in C#.

Methods to Establish Database Connection

The method 'OpenConnection' helps to create database connection. 'Con' is
connection object.


public SqlConnection Con;
public void OpenConnection()
{
string
cnstr="Server=YourServerName;Database=YourDatabaseName;Uid=YourUserId;Pwd=Yo
urPassword;";
Con=new SqlConnection(cnstr);
Con.Open();
}



Methods to Store and Retrieve Images

The method 'GetImage' gets the images that is stored in the database. The
method 'InsertImage' stores Images into database.


public byte[] GetImage(string ImageId)
{
string sql="Select Img_Bin from Images where Img_Code=@ImageId";
OpenConnection();
SqlCommand cmd=new SqlCommand(sql,Con);
cmd.Parameters.Add("@ImageId",SqlDbType.Int).Value=ImageId;
cmd.Prepare();
SqlDataReader dr=cmd.ExecuteReader();
dr.Read();
return (byte[])dr["Img_Bin"];
}


public void InsertImage(string ImgCode,byte[] Images,BigInt ContentLength)
{
string sql="Insert into Pot_Images (Img_Code,Img_Bin,Img_Len) values
(@ImgCode,@Images,@ImgLen)";
OpenConnection();
SqlCommand cmd=new SqlCommand(sql,Con);
cmd.Parameters.Add("@ImgCode",SqlDbType.Char,10).Value=ImgCode;
cmd.Parameters.Add("@Images",SqlDbType.Image,ContentLength).Value=Images;
cmd.Parameters.Add("@ImgLen",SqlDbType.BigInt).Value=ContentLength;
cmd.Prepare();
cmd.ExecuteNonQuery();
}



Inserting and Viewing Images from Webform
Double click the Submit button and copy the code below.


byte[] ImageStr=new byte[File1.PostedFile.ContentLength];
HttpPostedFile Image=File1.PostedFile;
Image.InputStream.Read(ImageStr,0,(int)File1.PostedFile.ContentLength);
InsertImage('1',ImageStr,File1.PostedFile.ContentLength);
Response.BinaryWrite(GetImage('1'));



Explanation of the Concept
Here we take a image in JPG,BMP or any format, and store it into database.
First we convert the image as binary stream with the help of
InputStream.Read method. Then we pass these information to insert into the
database. Then we can retrieve the binary data from database to view the
Image using Response.BinaryWrite method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,811
Latest member
SaulFernan

Latest Threads

Top