A netizen asked me this question this morning. I used to store file names directly in the database. I have not tried to store the whole picture in the database, and I searched the internet, I tested it again, Code As follows:
Create an SQL statement for the table that saves the image:
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 ( 1 , 1 ) 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 , Allow_page_locks = On ) On [ Primary ]
) On [ Primary ] Textimage_on [ Primary ]
The following code snippet saves the image to the database:
Int Len = Fu. postedfile. contentlength; // Image Size
Byte [] PIC = New Byte [Len]; // Create a byte array with the size of the image. This is stored in the database.
Fu. postedfile. inputstream. Read (PIC, 0 , Len ); // Read and save the files in the upload control in binary format to the PIC byte array.
// Insert images to 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 = " The image is successfully inserted into the database! " ;
Image1.imageurl = " Getpic. ashx? T = " + Datetime. Now. ticks; // Display the picture of the database just inserted
}
Finally
{
Connection. Close ();
}
The following is a code snippet for retrieving images from the database:
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, it refers to the process of converting an image into a byte array and storing it in the database. Then, it reads the byte array from the database, creates a stream through the byte array, and outputs the image through the over-stream, if you find that the image you saved to the database is a GIF image, you can convert it to a jpg image, because when you retrieve the image, we set its contenttype to image/JPEG.
Source code download: http://niunan.net/download/picsave2db.7z