Save images to the database in ASP. Net

Source: Internet
Author: User

Introduction to saving images to databases

In many cases, we need to store images in the database. In some applications, we may have some sensitive information. Some users may steal the information stored in the file system, 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?

Necessary Conditions for inserting an image

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 (including an image data type field) with 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:

Reference content is as follows:
Response. Write ("New person successfully added! ")
Catch SQLexc As SqlException
Response. Write ("Insert Failed. Error Details are:" & SQLexc. ToString ())
End Try

How does this work?
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 Read (in byte [] buffer, int offset, int count) method has three parameters. [For details about the Read method, see. Net FrameWorkSDK.] 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 of sqldbtype. image (datatype ). OK. After this is done, the image is successfully saved to SqlServer. The following is the written aspx page.

Conclusion

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.

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.