9CBS - Document Center - .NET Title In ASP.NET Save Image to SQL Server Database COMY (Translate) Keyword ASP.NET Image SQL Server Database Image Upload Home http: // http: //aspalliance.com/das/ INSERTIMAGE.ASPX
Save the picture to the SQL Server database in ASP.NET Translation: Lu Shaowei Introduction In many cases, we need to save the picture into 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 being inserted, implement the following method: intimagesize = personimage.postedfile.contentLENGTH
Then you want to get the picture type by the ContentYPE property. The last most important thing is to get the picture file stream, implement the following method: imageStream = personimage.postedFile.InputStream We have a Byte array imageContent, ready to save the image content. The entire picture is read through the READ method of the stream object. This method has three parameters, namely: # Copying image content target position # Read the start position # need to be read by the child read declaration as follows: intStatus = imageestream.read (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 the output sample: Save the picture to the SQL Server database test below the code code download Click here to download the ASPX page Click here to download the stored procedure summary, we have completed how to save the picture to the database. Discussions. 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 pictures from SQL Server, please refer to my article Retrieving Images from SQLServer in ASP .NET Translator Note: Due to the HTTP Transfer Protocol, the size of the file can be uploaded in different environments. Number. 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.