web.config
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <!-- 4 有关如何配置 ASP.NET 应用程序的详细信息,请访问 5 http://go.microsoft.com/fwlink/?LinkId=169433 6 --> 7 8 <configuration> 9 10 <system.web> 11 <compilation debug="true" targetFramework="4.5" /> 12 <httpRuntime targetFramework="4.5" /> 13 </system.web> 14 <connectionStrings> 15 <add connectionString="Data Source = .; Initial Catalog = mysql; Integrated Security = True;" name="conStr" /> 16 17 </connectionStrings> 18 </configuration>
UserInfo.html
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>用户信息</title> 6 <link href="tableStyle.css" rel="stylesheet" /> 7 </head> 8 <body> 9 <table> 10 <tr> 11 <th>id</th> 12 <th>姓名</th> 13 <th>邮箱</th> 14 <th>地址</th> 15 </tr> 16 $tbody 17 </table> 18 </body> 19 </html>
UserInfo.ashx
1 <%@ WebHandler Language="C#" Class="UserInfo" %> 2 3 using System; 4 using System.Web; 5 using System.Configuration; 6 using System.Data.SqlClient; 7 using System.Data; 8 using System.IO; 9 using System.Text; 10 11 public class UserInfo : IHttpHandler 12 { 13 14 public void ProcessRequest(HttpContext context) 15 { 16 context.Response.ContentType = "text/html"; 17 context.Response.Write("Hello World"); 18 //连接字符串 19 string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; 20 21 using (SqlConnection con = new SqlConnection(str)) 22 { 23 //获取数据库表格 24 using (SqlDataAdapter sda = new SqlDataAdapter("select * from student", con)) 25 { 26 //定义表格dt 27 DataTable dt = new DataTable(); 28 //将数据库的表格存入dt 29 sda.Fill(dt); 30 //定义sb来循环接收字符串 31 StringBuilder sb = new StringBuilder(); 32 for (int i = 0; i < dt.Rows.Count; i++) 33 { 34 //遍历表格dt,将每行数据作为字符串存入sb 35 sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><tr>", 36 dt.Rows[i]["id"].ToString(), dt.Rows[i]["name"].ToString(), dt.Rows[i]["email"].ToString(), dt.Rows[i]["address"].ToString()); 37 } 38 //获取html路径 39 string path = context.Request.MapPath("UserInfo.html"); 40 //获取html内容 41 string contentHtml = File.ReadAllText(path); 42 //把占位符部分替换 43 contentHtml = contentHtml.Replace("$tbody", sb.ToString()); 44 //输出网页 45 context.Response.Write(contentHtml); 46 } 47 48 } 49 } 50 51 public bool IsReusable 52 { 53 get 54 { 55 return false; 56 } 57 } 58 59 }
表格css样式
1 caption 2 { 3 padding: 0 0 5px 0; 4 700px; 5 font: italic 11px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif; 6 text-align: right; 7 } 8 9 th 10 { 11 font: bold 11px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif; 12 color: #burlywood; 13 border-right: 1px solid #C1DAD7; 14 border-left: 1px solid #C1DAD7; 15 border-bottom: 1px solid #C1DAD7; 16 border-top: 1px solid #C1DAD7; 17 letter-spacing: 2px; 18 text-transform: uppercase; 19 text-align: left; 20 padding: 6px 6px 6px 12px; 21 background: #0066AA no-repeat; 22 } 23 24 th.nobg 25 { 26 border-top: 0; 27 border-left: 0; 28 border-right: 1px solid #C1DAD7; 29 background: none; 30 } 31 32 td 33 { 34 border-left: 1px solid #C1DAD7; 35 border-right: 1px solid #C1DAD7; 36 border-bottom: 1px solid #C1DAD7; 37 background: #fff; 38 font-size: 11px; 39 padding: 6px 6px 6px 12px; 40 color: #4f6b72; 41 } 42 43 44 td.alt 45 { 46 background: #F5FAFA; 47 color: #797268; 48 } 49 50 th.spec 51 { 52 border-left: 1px solid #C1DAD7; 53 border-top: 0; 54 background: #fff no-repeat; 55 font: bold 10px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif; 56 } 57 58 th.specalt 59 { 60 border-left: 1px solid #C1DAD7; 61 border-top: 0; 62 background: #f5fafa no-repeat; 63 font: bold 10px "Trebuchet MS" , Verdana, Arial, Helvetica, sans-serif; 64 color: #797268; 65 } 66 67 .span_link 68 { 69 cursor:pointer; 70 color:Black; 71 } 72 73 .tr_Category 74 { 75 } 76 77 .pageLink 78 { 79 color:Blue; 80 margin-left:2px; 81 margin-right:2px; 82 } 83 84 .tr_Category_P td 85 { 86 background-color:#EEEEFF; 87 } 88 89