Asp_net: Extracts images from the SQL Server database and displays them in datagrid. Files

Source: Internet
Author: User

Reading images is much easier than storing images. To output an image, we must use the binarywrite method of the response object.

Set the image format. In this article, we will discuss how to retrieve images from sqlserver. I will also learn the following knowledge.

· How to set the image format?

· How to Use the binarywrite method.

We have stored data in the person table, so we will write some code to read data from the table.

The following code Retrieves all values from the person table.

Code for reading images from sqlserver.

Public sub page_load (sender as object, e as eventargs)
Dim myconnection as new sqlconnection (configurationsettings. etettings ("connectionstring "))
Dim mycommand as new sqlcommand ("select * From person", myconnection)
Try
Myconnection. open ()
Dim mydatareader as sqldatareader
Mydatareader = mycommand. executereader (commandbehavior. closeconnection)

Do While (mydatareader. Read ())
Response. contenttype = mydatareader. Item ("personimagetype ")
Response. binarywrite (mydatareader. Item ("personimage "))
Loop

Myconnection. Close ()
Response. Write ("person info successfully retrieved! ")
Catch sqlexc as sqlexception
Response. Write ("read failed:" & sqlexc. tostring ())
End try
End sub

How does he work?

The example above is simple. What we do is to execute an SQL statement and read all records again (looping through all the records ).

Before displaying an image, we first set the contenttype of the image, and then use the binarywrite method to output the image to the browser.

Source code:

/// Retriving. aspx

<% @ Page Language = "VB" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. sqlclient" %>
<HTML>
<Head>
<Title> retrieving image from the SQL server </title>
<SCRIPT runat = Server>
Public sub page_load (sender as object, e as eventargs)
'Create instance of connection and command object
Dim myconnection as new sqlconnection (configurationsettings. etettings ("connectionstring "))
Dim mycommand as new sqlcommand ("select * From person", myconnection)
Try
Myconnection. open ()
Dim mydatareader as sqldatareader
Mydatareader = mycommand. executereader (commandbehavior. closeconnection)

Do While (mydatareader. Read ())
Response. contenttype = mydatareader. Item ("personimagetype ")
Response. binarywrite (mydatareader. Item ("personimage "))
Loop

Myconnection. Close ()
Response. Write ("person info successfully retrieved! ")
Catch sqlexc as sqlexception
Response. Write ("read failed:" & sqlexc. tostring ())
End try
End sub

</SCRIPT>
</Head>
<Body style = "Font: 10pt verdana">
</Body>
</Html>

Related Article

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.