Use. NET to access SQL Server database images

Source: Internet
Author: User

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

 
 
  1. create table test   
  2. {   
  3. id identity(1,1),   
  4. FImage image   
  5. }   

Related stored procedures

 
 
  1. Create proc UpdateImage   
  2. (   
  3. @UpdateImage Image   
  4. )   
  5. As   
  6. Insert Into test(FImage) values(@UpdateImage)   
  7. GO   

Add the following to the UpPhoto. aspx file:

 
 
  1. < Input Id="UpPhoto" Name="UpPhoto" Runat="Server" Type="File">
  2. < 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:

 
 
  1. Private VoidBtnAdd_Click (ObjectSender, System. EventArgs e)
  2. {
  3. // Obtain the image and convert the image to byte [] 
  4. HttpPostedFile upPhoto = UpPhoto. PostedFile;
  5. IntUpPhotoLength = upPhoto. ContentLength;
  6. Byte[] PhotoArray =NewByte [upPhotoLength];
  7. Stream PhotoStream = upPhoto. InputStream;
  8. PhotoStream. Read (PhotoArray, 0, upPhotoLength );
  9. // Connect to the database 
  10. SqlConnection conn =NewSqlConnection ();
  11. Conn. ConnectionString ="Data Source = localhost; Database = test; User Id = sa; Pwd = sa";
  12. SqlCommand cmd =NewSqlCommand ("UpdateImage", Conn );
  13. Cmd. CommandType = CommandType. StoredProcedure;
  14. Cmd. Parameters. Add ("@ UpdateImage", SqlDbType. Image );
  15. Cmd. Parameters ["@ UpdateImage"]. Value = PhotoArray;
  16. // If you do not want to use the stored procedure to add an image, replace the above four statements with the following code: 
  17. // String strSql = "Insert into test (FImage) values (@ FImage )"; 
  18. // SqlCommand cmd = new SqlCommand (strSql, conn ); 
  19. // Cmd. Parameters. Add ("@ FImage", SqlDbType. Image ); 
  20. // Cmd. Parameters ["@ FImage"]. Value = PhotoArray; 
  21. Conn. Open ();
  22. Cmd. ExecuteNonQuery ();
  23. Conn. Close ();
  24. }

. NET image access techniques for SQL Server databases: Read and display from SQL Server

Add the following code to the image display area:

 
 
  1. < asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx">< /asp:image>  

ShowPhoto. aspx subject code:

 
 
  1. Private VoidPage_Load (ObjectSender, System. EventArgs e)
  2. {
  3. If(! Page. IsPostBack)
  4. {
  5. SqlConnection conn =NewSqlConnection ()
  6. Conn. ConnectionString ="Data Source = localhost; Database = test; User Id = sa; Pwd = sa";
  7. StringStrSql ="Select * from test where id = 2";// Assume that the image with id 2 is obtained. 
  8. SqlCommand cmd =NewSqlCommand (strSql, conn );
  9. Conn. Open ();
  10. SqlDataReader reader = cmd. ExecuteReader ();
  11. Reader. Read ();
  12. Response. ContentType ="Application/octet-stream";
  13. Response. BinaryWrite (Byte []) reader ["FImage"]);
  14. Response. End ();
  15. Reader. Close ();
  16. }
  17. }

The above describes how to access images from. NET to SQL Server databases.

  1. Common Methods for storing and reading. NET Binary Images
  2. Implementation of ASP. NET and SQL Server database image storage
  3. ASP. NET database image storage to Sql2000
  4. Implementation of ASP. NET database Image Upload and reading
  5. Analysis on adding watermarks to Images Using ASP. NET (VB)

Related Article

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.