Use. NET to access SQL Server database images
Using. Net technology, we can easily store images into the SQL Server database and conveniently read and display them. We will learn the detailed implementation methods step by step. First, how to store images in the SQL server database:
. NET: saving images to SQL Server databases
Use asp.net to upload images and store them in SQL Server. Then, read and display the images from SQL Server:
1) Upload and store the data to SQL Server
Database Structure
- create table test
- {
- id identity(1,1),
- FImage image
- }
Related stored procedures
- Create proc UpdateImage
- (
- @UpdateImage Image
- )
- As
- Insert Into test(FImage) values(@UpdateImage)
- GO
Add the following to the UpPhoto. aspx file:
- < Input Id="UpPhoto" Name="UpPhoto" Runat="Server" Type="File">
- < Asp: Button Id="BtnAdd" Name="BtnAdd" Runat="Server" Text="Upload"></Asp: Button>
Then, add the btnAdd button to the post code file UpPhoto. aspx. cs and click the event processing code:
- Private VoidBtnAdd_Click (ObjectSender, System. EventArgs e)
- {
- // Obtain the image and convert the image to byte []
- HttpPostedFile upPhoto = UpPhoto. PostedFile;
- IntUpPhotoLength = upPhoto. ContentLength;
- Byte[] PhotoArray =NewByte [upPhotoLength];
- Stream PhotoStream = upPhoto. InputStream;
- PhotoStream. Read (PhotoArray, 0, upPhotoLength );
- // Connect to the database
- SqlConnection conn =NewSqlConnection ();
- Conn. ConnectionString ="Data Source = localhost; Database = test; User Id = sa; Pwd = sa";
- SqlCommand cmd =NewSqlCommand ("UpdateImage", Conn );
- Cmd. CommandType = CommandType. StoredProcedure;
- Cmd. Parameters. Add ("@ UpdateImage", SqlDbType. Image );
- Cmd. Parameters ["@ UpdateImage"]. Value = PhotoArray;
- // If you do not want to use the stored procedure to add an image, replace the above four statements with the following code:
- // String strSql = "Insert into test (FImage) values (@ FImage )";
- // SqlCommand cmd = new SqlCommand (strSql, conn );
- // Cmd. Parameters. Add ("@ FImage", SqlDbType. Image );
- // Cmd. Parameters ["@ FImage"]. Value = PhotoArray;
- Conn. Open ();
- Cmd. ExecuteNonQuery ();
- Conn. Close ();
- }
. NET image access techniques for SQL Server databases: Read and display from SQL Server
Add the following code to the image display area:
- < asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx">< /asp:image>
ShowPhoto. aspx subject code:
- Private VoidPage_Load (ObjectSender, System. EventArgs e)
- {
- If(! Page. IsPostBack)
- {
- SqlConnection conn =NewSqlConnection ()
- Conn. ConnectionString ="Data Source = localhost; Database = test; User Id = sa; Pwd = sa";
- StringStrSql ="Select * from test where id = 2";// Assume that the image with id 2 is obtained.
- SqlCommand cmd =NewSqlCommand (strSql, conn );
- Conn. Open ();
- SqlDataReader reader = cmd. ExecuteReader ();
- Reader. Read ();
- Response. ContentType ="Application/octet-stream";
- Response. BinaryWrite (Byte []) reader ["FImage"]);
- Response. End ();
- Reader. Close ();
- }
- }
The above describes how to access images from. NET to SQL Server databases.
- Common Methods for storing and reading. NET Binary Images
- Implementation of ASP. NET and SQL Server database image storage
- ASP. NET database image storage to Sql2000
- Implementation of ASP. NET database Image Upload and reading
- Analysis on adding watermarks to Images Using ASP. NET (VB)