Save pictures to SQL Server database in asp.net

Source: Internet
Author: User
Tags config file size file system insert modify
asp.net|server| Data | Database Introduction
In many cases, we need to save the picture to the database. In some applications, there are sensitive information that cannot be stored in the file system because any pictures stored on the file system are easily accessible to users.

This article discusses how to save a picture to a SQL Server database in asp.net.

In this article we will look at the following areas:

L???????? Requirements for uploading picture files

L???????? Using the Strem object

L???????? Get upload picture size and type

L???????? How do I use the InputStream method?

Requirements for uploading picture files

We need to do two important things before we start the upload.

#Form标记的enctype属性需要被设置为如下形式:

Enctype= "Multipart/form-data"

#提供一个让用户选择图片文件的Html控件:



#还要引用System. IO namespaces to handle Strem objects

All three of these items are applied to the ASPX page. Some of the following requirements are also available in SQL Server:

#一个至少有一个字段类型为Image的表

#另外有一个用来存储图片类型的Varchar类型的字段就更好了

So, we have a datasheet with the Image field type and one (HTML file control). We also need a submit button that can be clicked when the user chooses a good picture. In the button onclick event we want to get the contents of the picture file and eventually insert it into the datasheet. Let's take a look at the button's onclick event, which reads the picture and inserts the picture into the datasheet.

OnClick event code for submit button

?

?? Dim Intimagesize as Int64
???? Dim Strimagetype as String
???? Dim ImageStream as Stream

????' Gets the Size of the Image
???? Intimagesize = PersonImage.PostedFile.ContentLength

????' Gets the Image Type
???? Strimagetype = PersonImage.PostedFile.ContentType

????' Reads the Image
???? ImageStream = PersonImage.PostedFile.InputStream

???? Dim imagecontent (intimagesize) as Byte
???? Dim Intstatus as Integer
???? Intstatus = Imagestream.read (imagecontent, 0, Intimagesize)

????' Create Instance of Connection and Command Object
???? Dim MyConnection as New SqlConnection (ConfigurationSettings.AppSettings ("ConnectionString"))
???? Dim mycommand as New SqlCommand ("Sp_person_isp", MyConnection)

????' Mark the Command as a sproc
???? myCommand.CommandType = CommandType.StoredProcedure

????' Add Parameters to SPROC
???? Dim Prmpersonimage as New SqlParameter ("@PersonImage", Sqldbtype.image)
???? Prmpersonimage.value = Imagecontent
???? MYCOMMAND.PARAMETERS.ADD (Prmpersonimage)

???? Dim Prmpersonimagetype as New SqlParameter ("@PersonImageType", SqlDbType.VarChar, 255)
???? Prmpersonimagetype.value = Strimagetype
???? MYCOMMAND.PARAMETERS.ADD (Prmpersonimagetype)

???? Try
???????? Myconnection.open ()
???????? Mycommand.executenonquery ()
???????? Myconnection.close ()
???????? Response.Write ("New person successfully added!")
???? Catch Sqlexc as SqlException
???????? Response.Write ("Insert Failed.") Error Details are: "& Sqlexc.tostring ())
???? End Try

?

How does it work?

Object Personimage is a HtmlInputFile control. First, we want to get the size of the inserted picture, which is implemented by the following methods:

Intimagesize = PersonImage.PostedFile.ContentLength

Then you get the picture type through the Contentype property. Finally, the most important thing is to get the picture file stream, which is done in the following ways:

ImageStream = PersonImage.PostedFile.InputStream

We have a byte array imagecontent, ready to be used to save the picture content. The entire picture is read by the Read method of the Stream object, which has three parameters, namely:

#被复制的图片内容的目标位置

#读的开始位置

#需要被读的子节数

Read the statement as follows:

Intstatus = Imagestream.read (imagecontent, 0, Intimagesize)

Now, we read the entire picture content. Then we need to insert the picture into the SQL datasheet, and we'll use a stored procedure to insert the picture type and picture into the SQL datasheet. If you look at the code list above, you know that we set the data type to sqldbtype.image. In this way, we successfully saved the picture to the SQL Server database.

Example of an output sample



Picture: Saving pictures to SQL Server database

Test the following code

Code download

Click here to download aspx page

Click here to download the stored procedure

Summarize

In this way, we have finished discussing how to save the picture to the database. We are also ready to use the examples and stored procedures provided in the download section above. If you want to know how to read pictures from SQL Server, see my article retrieving Images from SQL Server in ASP. NET

Translator Note: Due to HTTP transport protocol restrictions, in different environments can upload file size. This is not a perfect solution for Web applications that want to upload large files, but it provides us with a relatively good approach. I have used this method to successfully upload 30M files in a local area network, but only 5M of data can be uploaded in another native system with low system performance. In addition, readers should note that when uploading large files, you need to modify the Machine.config and Web.config files appropriately, as long as you open the files to know how to modify the appropriate. The first translation, but also hope that everyone to correct me.




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.