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
Explain About remoting and web services. Difference between them
What is an inheritance in c#?
What is the difference between iqueryable and ienumerable?
Is array reference type in c#?
What are data types in C#?
Can you pass parameters to destructors?
What is default method in c#?
What is difference between property and variable in c#?
Can derived classes have greater accessibility than their base types?
What is private protected in c#?
What does void mean in c#?
How do I enforce const correctness in c#?
How many kinds of elements an array can have?
what is the difference between interface and multiple interface?
What floating point types is supported in C#?