Asp.net displays the image code in the database

Source: Internet
Author: User

& Lt; % @ Page Language = & quot; C # & quot; % & gt; & lt; % @ Import Namespace = & quot; System. data. oleDb & quot; % & gt; & lt; % @ Import Namespace = & quot; System. data. sqlClient & quot; % & gt; & lt; script runat = & quot; server & quot; & gt; protected void Page_Load (object sender, EventArgs e) {//// create a database connection, SQL statement, and create a parameter // ACCESS the database using this annotation section // string strCnn = & quot; Provider = Microsoft. jet. OLEDB.4.0; Data Source = & quot; + Server. mapPath (& quot; Image2Access. mdb & quot;); // OleDbConnection myConnection = new OleDbConnection (strCnn); // OleDbCommand command = new OleDbCommand (& quot; select * from Person Where PersonID = & quot; + Request. queryString [& quot; id & quot;], myConnection); // myConnection. open (); // OleDbDataReader dr = command. executeReader (); // if (dr. read () // {// Response. clear (); // Response. addHeader (& quot; Content-Type & quot;, dr [& quot; PersonImageType & quot;]. toString (); // Response. binaryWrite (byte []) dr [& quot; PersonImage & quot;]); //} // dr. close (); // myConnection. dispose (); // construct a database connection, SQL statement, create the parameter string strCnn = & quot; Persist Security Info = False; User ID = sa; Password =; Initial Catalog = Book; server = (local); & quot; SqlConnection myConnection = new SqlConnection (strCnn); SqlCommand command = new SqlCommand (& quot; select * from UserPhoto Where id = & quot; + Request. queryString [& quot; id & quot;], myConnection); myConnection. open (); SqlDataReader dr = command. executeReader (); if (dr. read () {Response. clear (); Response. addHeader (& quot; Content-Type & quot;, dr [& quot; ContentType & quot;]. toString (); Response. binaryWrite (byte []) dr [& quot; Photo & quot;]);} dr. close (); myConnection. dispose () ;}& lt;/script & gt;

 

Create SQL data table statements

 

Create table [UserPhoto] ([id] [int] IDENTITY (1, 1) not null, [UserName] [nvarchar] (255) COLLATE Chinese_PRC_CI_AS not null, [ContentType] [varchar] (50) COLLATE Chinese_PRC_CI_AS not null, [Photo] [image] not null, CONSTRAINT [PK_UserPhoto] primary key clustered ([id]) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO

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.