How to deposit the image into the database in ASP.NET

zhaozj2021-02-16  56

Introduction

When there may be a lot, we urgently need to store the picture into the database. In some applications, we may have some sensitive information, which will be easily stolen by certain users because of something stored in the file system. So these data cannot be stored in the file system.

In this article, we will discuss how to store pictures into SQL2000.

In this article we can learn the following knowledge:

1. The necessary conditions inserted into the picture

2. Using the flow object

3. Find the size and type of the image ready to be uploaded

4. How to use the InputStream method?

Sufficient condition for inserting images

Before we start upload, there are two important things we need to do:

#Form Tag ENCTYPE attribute should be set to encType = "Multipart / Form-Data"

# Need a form to make the user choose the file they want to upload, and we need to import the system.io namespace to process the flow object.

Apply the above three points to the ASPX page. At the same time, we need to prepare SQL Server.

# Require at least a table of fields with a picture type

# If we have another field of variable character type to store the picture type, it will be better.

Now we have a SQL table (including a field of an image data type), as well as the tag. Of course we have to prepare the Submit button so that the user is submitted after the image is selected. In the onclick event of this button, we need to read the contents of the selected image, then store it in the table. Then let's take a look at this onclick event.

The code of 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 ("@ per sonImageType ", 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.toTString ()) How does this work?

PersonImage is an object of the HTMLInputFile control. First, you need to get the size of the picture, you can use the following code:

Intimagesize = personimage.postedfile.contentLength

Then return to the type of image Use the ContentYpe property. Finally, it is also the most important thing to get image stream, which can be implemented in the following code:

Images.postedFile.InputStream We need a byte array to store image content. Reading the entire image can be implemented using the READ method of the Stream object. The read (in Byte [] buffer, int off version is three parameters. [About the read method can be found .NET Frameworksdk] They are:

Buffer

Byte arrays. When this method returns, the buffer contains the specified character array, and the value between the array OFFSET and the OFFSET COUNT is replaced by the byte read from the current source.

Offset

The byte offset from zero starts from the buffer starts to store data read from the current stream from here.

count

The number of bytes to be read from the current stream.

This read method implements the following code: intStatus = imageStream.Read (ImageContent, 0, Intimagesize).

Now, we have read the content of the entire picture, the next step, we have to store these contents into the SQL table. We will use the stored procedure to complete the inserted image type and the image content to the SQL table. If you browse the above code, you will find that we use the data type of SqldbType.Image (DataType). OK, complete this, we also put the picture into the SQLServer. Here is the ASPX page we have written.

in conclusion

We have discussed how to store pictures into SQL Server, then how do we read images from SQL Server? You can see another article: retrieve the picture from SQL Server in ASP.NET.

转载请注明原文地址:https://www.9cbs.com/read-24902.html

New Post(0)