Asp. NET allows images to be stored in a binary form in the database

Source: Internet
Author: User

This morning a netizen asked me this question, before I was directly in the database file name, has not tried to store the whole picture into the database, the Internet search for a bit, and tested some of them, the code is as follows:
Create the SQL statement for the table that holds the picture:

SQL code
  1. Use [Niunantest]
  2. GO
  3. /****** object: Table [dbo].[ Picdata] Script Date: 03/30/2010 14:51:58 ******/
  4. SET ansi_nulls on
  5. GO
  6. SET quoted_identifier on
  7. GO
  8. CREATE TABLE [dbo].[ Picdata] (
  9. [ID] [int] IDENTITY (*) not NULL,
  10. [Content] [Image] NULL,
  11. [CreateDate] [DateTime] not NULL CONSTRAINT [df_picdata_createdate] DEFAULT (getdate ()),
  12. CONSTRAINT [Pk_picdata] PRIMARY KEY CLUSTERED
  13. (
  14. [id] ASC
  15. ) with(Pad_index = off, Statistics_norecompute = off, Ignore_dup_key = off, allow_row_locks = on , A Llow_page_locks = on ) on [PRIMARY]
  16. ) on [PRIMARY] textimage_on [PRIMARY]

Here is the code snippet to save the picture to the database:

C # code
  1. int len = fu.  Postedfile.contentlength; //Picture size
  2. byte[] pic = new Byte[len]; //Create a byte array, size is the size of the picture, the database stores this thing
  3. Fu. PostedFile.InputStream.Read (pic, 0, Len); //The file in the upload control is stored in a binary read in the pic byte array
  4. Insert a picture into the database
  5. SqlConnection connection = new
  6. SqlConnection (@"server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456");
  7. Try
  8. {
  9. Connection. Open ();
  10. SqlCommand cmd = new SqlCommand ("insert into Picdata"
  11. + "([content]) VALUES (@pic)", connection);
  12. Cmd.  Parameters.Add ("@pic", pic);
  13. Cmd. ExecuteNonQuery ();
  14. Label1.Text = "picture inserted database succeeded!"  ";
  15. Image1.imageurl = "getpic.ashx?t=" + DateTime.Now.Ticks; //Display pictures just inserted into the database
  16. }
  17. Finally
  18. {
  19. Connection. Close ();
  20. }

Here is a snippet of the picture taken from the database:

C # code
  1. MemoryStream stream = new MemoryStream ();
  2. SqlConnection connection = new
  3. SqlConnection (@"server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456");
  4. Try
  5. {
  6. Connection. Open ();
  7. SqlCommand command = new
  8. SqlCommand ("SELECT top 1 [content] from Picdata ORDER by id DESC", connection);
  9. byte[] image = (byte[]) command.  ExecuteScalar ();
  10. Stream. Write (image, 0, image. Length);
  11. Bitmap Bitmap = new Bitmap (stream);
  12. Context.  Response.ContentType = "Image/jpeg";
  13. Bitmap. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);
  14. }
  15. Finally
  16. {
  17. Connection. Close ();
  18. Stream. Close ();
  19. }

In fact, through the flow of the picture into a byte array to the database, and then read the byte array from the database, and then through the byte array to create a stream, and then through the flow of the image output, found that you stored in the database is a GIF image, and then take out the image can be converted to JPG, Because when we take out the image, we set his contenttype to be image/jpeg.


SOURCE Download: http://niunan.net/download/picsave2db.7z

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.