How to store image file in Sql server database?

Answer Posted / gp_bellamkonda

=====Here is the sample for image storage and retrieve===

In the OnClick property of the button you can write the
following code to upload an image to the database.

// create a byte[] for the image file that is uploaded
int imagelen = Upload.PostedFile.ContentLength;
byte[] picbyte = new byte[imagelen];
Upload.PostedFile.InputStream.Read (picbyte, 0, imagelen);
// Insert the image and image id into the database
SqlConnection conn = new SqlConnection (@"give the
connection string here...");
try
{
conn.Open ();
SqlCommand cmd = new SqlCommand ("insert into ImageTable "
+ "(ImageField, ImageID) values (@pic, @imageid)", conn);
cmd.Parameters.Add ("@pic", picbyte);
cmd.Parameters.Add ("@imageid", lblImageID.Text);
cmd.ExecuteNonQuery ();
}
finally
{
conn.Close ();
}

You can also write the above code in a function and call
that function in the OnClick event of the upload button.
The code given above performs the following steps in the
process of inserting an image into the database.

1. Get the content length of the image that is to be
uploaded
2. Create a byte[] to store the image
3. Read the input stream of the posted file
4. Create a connection object
5. Open the connection object
6. Create a command object
7. Add parameters to the command object
8. Execute the sql command using the ExecuteNonQuery method
of the command object
9. Close the connection object

To retrieve the image from the SQL Database you can perform
the following steps.

1. Create a MemoryStream object. The code can be something
like,
MemoryStream mstream = new MemoryStream ();
2. Create a Connection object
3. Open the connection to the database
4. Create a command object to execute the command to
retrieve the image
5. Use the command object’s ExecuteScalar method to
retrieve the image
6. Cast the output of the ExecuteScalar method to that of
byte[]
byte[] image = (byte[]) command.ExecuteScalar ();
7. Write the stream
mstream.Write (image, 0, image.Length);
8. Create a bitmap object to hold the stream
Bitmap bitmap = new Bitmap (stream);
9. Set the content type to “image/gif”
Response.ContentType = "image/gif";
10. Use the Save method of the bitmap object to output the
image to the OutputStream.
bitmap.Save (Response.OutputStream, ImageFormat.Gif);
11. Close the connection
12. Close the stream
mstream.Close();

Using the above steps you can retrieve and display the
image from the database to the web page.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the process of Serialization?

544


How does split work in c#?

484


Which class comes after the SortedList class?

566


What does type safety mean?

573


What is remote data?

467






Tell us something about static linking and dynamic linking?

529


How will you allow a class to be inherited, but prevent the method from being over-ridden?

522


What is the difference between a field and a property in c#?

490


Explain what is copy constructor?

548


how to implement a web service in .net

468


What is a class c#?

475


What do you mean by for each loop?

478


How does a while loop work?

472


what happens if you inherit multiple interfaces and they have conflicting method names?

486


What is the purpose of namespace?

465