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
- Use [Niunantest]
- GO
- /****** object: Table [dbo].[ Picdata] Script Date: 03/30/2010 14:51:58 ******/
- SET ansi_nulls on
- GO
- SET quoted_identifier on
- GO
- CREATE TABLE [dbo].[ Picdata] (
- [ID] [int] IDENTITY (*) not NULL,
- [Content] [Image] NULL,
- [CreateDate] [DateTime] not NULL CONSTRAINT [df_picdata_createdate] DEFAULT (getdate ()),
- CONSTRAINT [Pk_picdata] PRIMARY KEY CLUSTERED
- (
- [id] ASC
- ) with(Pad_index = off, Statistics_norecompute = off, Ignore_dup_key = off, allow_row_locks = on , A Llow_page_locks = on ) on [PRIMARY]
- ) on [PRIMARY] textimage_on [PRIMARY]
Here is the code snippet to save the picture to the database:
C # code
- int len = fu. Postedfile.contentlength; //Picture size
- byte[] pic = new Byte[len]; //Create a byte array, size is the size of the picture, the database stores this thing
- Fu. PostedFile.InputStream.Read (pic, 0, Len); //The file in the upload control is stored in a binary read in the pic byte array
- Insert a picture into the database
- SqlConnection connection = new
- SqlConnection (@"server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456");
- Try
- {
- Connection. Open ();
- SqlCommand cmd = new SqlCommand ("insert into Picdata"
- + "([content]) VALUES (@pic)", connection);
- Cmd. Parameters.Add ("@pic", pic);
- Cmd. ExecuteNonQuery ();
- Label1.Text = "picture inserted database succeeded!" ";
- Image1.imageurl = "getpic.ashx?t=" + DateTime.Now.Ticks; //Display pictures just inserted into the database
- }
- Finally
- {
- Connection. Close ();
- }
Here is a snippet of the picture taken from the database:
C # code
- MemoryStream stream = new MemoryStream ();
- SqlConnection connection = new
- SqlConnection (@"server=.\sqlexpress;database=niunantest;uid=sa;pwd=123456");
- Try
- {
- Connection. Open ();
- SqlCommand command = new
- SqlCommand ("SELECT top 1 [content] from Picdata ORDER by id DESC", connection);
- byte[] image = (byte[]) command. ExecuteScalar ();
- Stream. Write (image, 0, image. Length);
- Bitmap Bitmap = new Bitmap (stream);
- Context. Response.ContentType = "Image/jpeg";
- Bitmap. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- Finally
- {
- Connection. Close ();
- Stream. Close ();
- }
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