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.