Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

What is an expression in c#?

862


What is the data type for bit in c#?

897


What is iqueryable in c#?

846


What is xamarin used for?

886


Are private members inherited in c#?

926


How do you declare a variable in c#?

930


What is static void main in c#?

950


what is the scope of anonymous type ?

961


Can scriptable objects have methods?

850


What type is string in c#?

829


What is the function of the not null constraint?

903


What is the difference between CreateObject() and GetObject()?

1041


Can firstordefault return null?

895


What is difference between dictionary and hashtable in c#?

829


What are custom exceptions?

877