ASP. NET implementation images are stored in the database in binary form

  • 2021-01-02 21:48:34
  • OfStack

This paper presents an example of ASP.NET's method of storing images in base 2. In the past, we directly stored the image file name in the database, but we haven't tried to store the entire image in the database. After a data query and test, we sorted out the following functional codes:

1. SQL statement to create a table to save images:


USE [niunantest] 
GO 
/******  object : Table [dbo].[picdata]   Date of the script : 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] 

2. The following is the code snippet to save the picture to the database:


int len = fu.PostedFile.ContentLength; //  Image size  
byte[] pic = new byte[len]; //  create 1 An array of two bytes, the size of the image, that's what's stored in the database  
fu.PostedFile.InputStream.Read(pic, 0, len); //  Use the file in the upload control 2 Base read save to pic Byte array  
//   Insert the image 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 = " Image inserted into database successfully! "; 
 
  Image1.ImageUrl = "getpic.ashx?t=" + DateTime.Now.Ticks; //  Displays the image that you just inserted into the database  
} 
finally 
{ 
  connection.Close(); 
}  
 

3. The following is the code snippet of the image taken from the database:


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(); 
}

The principle of program is also is through flow into a byte array to save the image in the database, and then come out from an array of bytes read from the database, and then create flow through an array of bytes, again through the image output flow out, found that you save to the database is gif image again out can put him into jpg images, because when we set out in the image is his ContentType image/jpeg.


Related articles: