ASP. NET database image storage to Sql2000

Source: Internet
Author: User

In many cases, we need to store images in the database. In some applications, we may have some sensitive information, because the content stored in the file system of the file system will be easily stolen by some users, therefore, the data cannot be stored in the file system.

In this article, we will discuss how to store images in Sql2000.

In this article, we can learn the following knowledge:

1. Necessary Conditions for inserting an image

2. Use stream objects

3. Search for the size and type of the image to be uploaded

4. How to Use the InputStream method?

ASP. NET database image storage: Necessary Conditions for inserting Images

Before we start uploading, there are two important things we need to do:

# Set the enctype attribute marked by Form to enctype = "multipart/form-data"

# A <input type = file> form is required to allow users to select the files they want to upload. At the same time, we need to import the System. IO namespace to process stream objects.

Apply the preceding three points to the aspx page. At the same time, we need to make the following preparations for SqlServer.

# A table that must contain at least one field of the image type

# It would be better if we had another character type field to store the image type.

Now, we have prepared an SQL table containing an image data type field) and the <input type = file> mark. Of course, we have to prepare the Submit button so that the user can Submit the image after selecting it. In The Onclick event of this button, we need to read the content of the selected image and store it in the table. Let's take a look at this Onclick event.

Code for The Onclick event of the submit button:

 
 
  1. Dim intImageSize As Int64   
  2.      Dim strImageType As String   
  3.      Dim ImageStream As Stream  
  4.  
  5.     ’ Gets the Size of the Image   
  6.     intImageSize = PersonImage.PostedFile.ContentLength  
  7.  
  8.     ’ Gets the Image Type   
  9.     strImageType = PersonImage.PostedFile.ContentType  
  10.  
  11.     ’ Reads the Image   
  12.     ImageStream = PersonImage.PostedFile.InputStream  
  13.  
  14.     Dim ImageContent(intImageSize) As Byte   
  15.     Dim intStatus As Integer   
  16.     intStatus = ImageStream.Read(ImageContent, 0, intImageSize)  
  17.  
  18.     ’ Create Instance of Connection and Command Object   
  19.     Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))   
  20.     Dim myCommand As New SqlCommand("sp_person_isp", myConnection)  
  21.  
  22.     ’ Mark the Command as a SPROC   
  23.     myCommand.CommandType = CommandType.StoredProcedure  
  24.  
  25.     ’ Add Parameters to SPROC   
  26.     Dim prmPersonImage As New SqlParameter("@PersonImage", SqlDbType.Image)   
  27.     prmPersonImage.Value = ImageContent   
  28.     myCommand.Parameters.Add(prmPersonImage)  
  29.  
  30.     Dim prmPersonImageType As New SqlParameter("@PersonImageType", SqlDbType.VarChar, 255)   
  31.     prmPersonImageType.Value = strImageType   
  32.     myCommand.Parameters.Add(prmPersonImageType)  
  33.  
  34.     Try   
  35.         myConnection.Open()   
  36.         myCommand.ExecuteNonQuery()   
  37.         myConnection.Close()  
  38.        Response.Write("New person successfully added!")   
  39.     Catch SQLexc As SqlException   
  40.         Response.Write("Insert Failed. Error Details are: " & SQLexc.ToString())   
  41.     End Try 

How does this implement ASP. NET database image storage?

PersonImage is the object of the HTMLInputFile control. First, you need to obtain the image size, which can be implemented using the following code:

IntImageSize = PersonImage. PostedFile. ContentLength

The returned image type uses the ContenType attribute. Finally, the most important thing is to get the Image Stream, which can be implemented using the following code:

ImageStream = PersonImage. PostedFile. InputStream

We need a byte array to store the image content. You can use the Stream object's Read method to Read the entire image. The Readin byte [] buffer, int offset, int count) method has three parameters. They are:

Buffer

Byte array. When this method is returned, the buffer contains the specified character array. The value between the offset and (offset + count) of the array is replaced by the byte read from the current source.

Offset

The Byte offset starting from zero in the buffer, from which data read from the current stream is stored.

Count

The maximum number of bytes to read from the current stream.

This Read method is implemented using the following code:
IntStatus = ImageStream. Read (ImageContent, 0, intImageSize)
.

Now we have read the content of the entire image. Next, we will save the content to the SQL table. We will use the stored procedure to insert the image type and image content to the SQL table. If you browse the above Code, you will find that we use the data type datatype of sqldbtype. image ). OK. After this is done, the image is successfully saved to SqlServer. The following is the written aspx page.

ASP. NET database image storage: conclusion of saving images to the database

We have discussed how to store images to SQL Server. How can we read images from SQL Server? See my other article: retrieving images from SqlServer in Asp. Net.

  1. Add an ASP. NET custom error handling page
  2. Application of session storage mode in ASP. NET
  3. Set of methods for uploading and downloading files in ASP. NET
  4. Introduction to cookie read/write methods in ASP. NET
  5. Javascript operations in ASP. NET

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.