Type of the image field read from the database manually

Source: Internet
Author: User

Background code:

 

1 using system;
2 using system. configuration;
3 using system. Data;
4 using system. LINQ;
5 using system. Web;
6 using system. Web. Security;
7 using system. Web. UI;
8 using system. Web. UI. htmlcontrols;
9 using system. Web. UI. webcontrols;
10 using system. Web. UI. webcontrols. webparts;
11 using system. xml. LINQ;
12 using system. Data. sqlclient;
13 using system. IO;
14
15 public partial class _ default: system. Web. UI. Page
16 {
17 protected void page_load (Object sender, eventargs E)
18 {
19 // establish a database connection
20 sqlconnection con = new sqlconnection (@ "Data Source =. \ sqlexpress; attachdbfilename = c: \ myasp \ modular website development \ chpater6 \ app_data \ upfile_data.mdf; Integrated Security = true; Connect timeout = 30; user instance = true "); // local SQL Server database upfile
21 string sqlcmd = "select * From imagestore where imageid = 1"; // define the query string
22 sqlcommand cmdobj = new sqlcommand (sqlcmd, con); // defines the sqlcommand object instance
23 con. open (); // open the database connection
24
25 try
26 {
27
28 // read data from the database
29 sqldatareader sqlreader = cmdobj. executereader (); // defines the sqldatareader object instance
30 sqlreader. Read (); // read data from the database in read-only mode
31
32 // output data to the HTTP stream
33 response. contenttype = (string) sqlreader ["imagecontenttype"]; // set the output file type
34 response. outputstream. Write (byte []) sqlreader ["imagedata"], 0, (INT) sqlreader ["imagesize"]); // outputs the binary number of the image file
35 response. End (); // end the program
36 con. Close (); // closes the database connection object
37}
38 catch
39 {
40 response. End (); // end the program
41 con. Close (); // closes the database connection object
42}
43}
44}
45

 

 

 

Front-end code:

Default value.

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.