In many cases, we need to store images in the database. In some applications, we may have some sensitive information, because the content stored in the file system of the file system will be easily stolen by some users, 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?
ASP. NET database image storage: Necessary Conditions for inserting Images
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 containing an image data type field) and 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:
- 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 this implement ASP. NET database image storage?
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 Readin byte [] buffer, int offset, int count) method has three parameters. 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 datatype of sqldbtype. image ). OK. After this is done, the image is successfully saved to SqlServer. The following is the written aspx page.
ASP. NET database image storage: conclusion of saving images to the database
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.
- Add an ASP. NET custom error handling page
- Application of session storage mode in ASP. NET
- Set of methods for uploading and downloading files in ASP. NET
- Introduction to cookie read/write methods in ASP. NET
- Javascript operations in ASP. NET