Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to store ASP.NET database pictures in Sql2000

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how ASP.NET database images are stored in Sql2000. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

ASP.NET database picture storage: a necessary condition for inserting pictures

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

# the enctype attribute of the Form tag should be set to enctype= "multipart/form-data"

# We need a form to enable users to select the files they want to upload, and we need to import the System.IO namespace to handle stream objects

Apply the above three points to the aspx page. At the same time, we need to make the following preparations for SqlServer.

# A table containing at least one field of picture type is required

# it would be better if we had another field with variable character type to store the image type.

Now we have a Sql table (containing a field of image data type) and tags. Of course, we also 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 contents of the selected image and store it in the table. Let's first take a look at this Onclick incident.

The 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 ImageType 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 realize the storage of pictures in ASP.NET database?

PersonImage is the object of the HTMLInputFile control. First, you need to get the size of the picture, which can be achieved using the following code:

IntImageSize = PersonImage.PostedFile.ContentLength

Then return to the type of picture using the ContenType property. *, and the most important thing is to get the Image Stream, which can be achieved with the following code:

ImageStream = PersonImage.PostedFile.InputStream

We need a byte array to store the image content. Reading the entire picture can be done using the Read method of the Stream object. The Read (in byte [] buffer,int offset,int count) method takes three parameters. They are:

Buffer

Byte array. When this method returns, the buffer contains the specified array of characters whose values between offset and (offset + count) are replaced by bytes read from the current source.

Offset

The zero-based byte offset in buffer, where 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 in the following code:

IntStatus = ImageStream.Read (ImageContent, 0, intImageSize)

.

Now that we have read the contents of the entire picture, the next step is to store these contents in the sql table. We will use stored procedures to insert the image type and content into the sql table. If you browse the above code, you will find that we use the sqldbtype.image data type (datatype). Ok, completed these, we also successfully put the picture into the SqlServer.

Thank you for reading! This is the end of this article on "how to store ASP.NET database pictures in Sql2000". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report