Save the picture to the SQL Server database in ASP.NET
Translation: Lu Shaowei
Introduction In many cases, we need to save the picture to the database. In some applications, some sensitive information cannot be stored in a file system because any picture stored on the file system is easily illegally obtained by the user. This article will discuss how to save pictures into the SQL Server database in ASP.NET. In this paper, we will learn about the following aspects: l Upload image files LEAR DETECTION LET LEAR SYSTEM MET Uploading image file requirements Before loading, we need two important things # form attributes of two important things #Form tags need to be set as follows: enctype = "multipart / form-data
# Provide an HTML control that allows users to choose a picture file:
# Also refer to the system.io namespace to handle the three items described above to the ASPX page. There are some of the following requirements in SQL Server: # a table with at least one field type to image #, there is a field of a varchar type for storing the picture type, we have a type of image field type. Data table and one (HTML file control). We also need a submission button, and you can click on it after selecting a picture. In the button's onclick event we have to get the contents of the image file and eventually insert it into the data table. Let's take a look at the button's onclick event, which read the picture and insert the image into the data table.
ONCLICK event code for submitting buttons
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 ("@ PERSO nImageType ", 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 an HTMLInputFile control. First we have to get the size of the picture, achieve:
Intimagesize = personimage.postedfile.contentLength
Then you want to get the picture type by the ContentYPE property. Finally, the most important thing is to get the picture file stream, achieved by the following method:
Images.postedFile.inputStream We have a BYTE array imageContent, ready to save image content. The entire picture is read through the READ method of the Stream object, which has three parameters, namely:
# Target location of the copyed image content
# Read the start position
# Number of childs needed to be read
The reading declaration is as follows:
IntStatus = images (ImageContent, 0, IntimageSize)
Now we read the entire picture content. Then we need to insert the image into the SQL data table, we will use a stored procedure to insert the picture type and the image into the SQL data sheet. If you have seen the list of code 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 output sample
Figure: Save the picture to the SQL Server database
Test the code below
Code download
Click here to download the ASPX page
Click here to download the stored procedure
to sum up
In this way, we have completed how to save pictures to the database. We are also ready to use the examples and stored procedures provided on the download section above. If you want to know how to read images from SQL Server, please refer to my article Retrieving Images from Sql Server In Asp .NET
Translator Note: Due to the limitations of the HTTP transport protocol, the size of the file can be uploaded in different environments. This is not a perfect solution for web applications to upload large files, just provide a relatively good method. I used this method to successfully upload 30m files in the LAN, but in another native system with lower system performance, up to 5M data. In addition, readers need to pay attention to it, to properly modify the machine.config and web.config files when uploading big files, as long as you open these files, you can know how appropriate modifications. I hope that everyone will refer to it.