Implementation Code for storing ASP. NET images into a database

Source: Internet
Author: User

In this article 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?
Necessary Conditions for inserting an image
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 (including an image data type field) with 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.
The onclick event of the submit button Code :
Reference content is as follows: Copy code The Code is as follows: 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. etettings ("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 work?
personimage is the object of the htmlinputfile control. To obtain the image size, use the following code:
intimagesize = personimage. postedfile. contentlength
and then return the image type using 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 read (in byte [] buffer, int offset, int count) method has three parameters. [For details about the read method, see. Net frameworksdk.] These are:
buffer
byte arrays. 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 zero-beginning byte offset in the buffer, from which data read from the current stream is stored.
count
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 of sqldbtype. Image (datatype ). OK. After this is done, the image is successfully saved to sqlserver. The following is the written ASPX page.
conclusion:
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.

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.