Save pictures to SQL Server database in asp.net

Source: Internet
Author: User
Introduced
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

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.