应用ASP.Net在web中查询数据库
2017-03-07 19:31
288 查看
将数据库中的数据查询出来,并打印在html页面上
首先创建一个website,在web.config文件中写上连接字符串<connectionStrings>
<add name="constr" connectionString="Data Source=Graph;Initial Catalog=化学类一班;Integrated Security=True;"/></connectionStrings> -----身份验证方式用的是windows身份验证,name属性是不可或缺的
新建一个html页面和一个一般处理程序,由于本次是将数据库中的数据一行一行打印在html上,肯定要用到循环,可以在html页面上写一个$tbody当做占位符
在一般处理程序中,首先要拿到web.config里面的连接字符串并建立数据库连接string constring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;using(SqlConnection conn=new SqlConnection(constring)){} //用using可以自动释放不在需要的对象的内存
然后建立SqlDataAdapter对象using (SqlDataAdapter adapter=new SqlDataAdapter ("select *from 学生登记表",conn)){} 因为SqlDataAdapter存在重载,可以将SQL语句当做参数写进去。
建立DataTable对象,DataTable的对象是一个虚拟表格,将从数据库中拿到的数据放在DataTable对象中DataTable da = new DataTable();
建立StringBuilder对象,他有一个AppendFormat方法可以比较方便在<td></td>中插入数据StringBuilder sb = new StringBuilder();
循环遍历 for (int i = 0; i < da.Rows.Count; i++) { sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", da.Rows[i]["autoId"], da.Rows[i]["学号"],
da.Rows[i]["姓名"]);}将对象da里面的每一行数据的分别拿到,用da.Rows[i]["列名"]可以拿到,然后放在td标签里面
最后要建立一般处理程序和html页面的连接string filePath = context.Request.MapPath("index.html");
string fileContent = File.ReadAllText(filePath);
fileContent= fileContent.Replace("$tbody", sb.ToString());//替换html页面中的$tbody占位符
context.Response.Write(fileContent);//打印
下面是源码:一班处理程序 indexList.ashx
web.config
index.html
首先创建一个website,在web.config文件中写上连接字符串<connectionStrings>
<add name="constr" connectionString="Data Source=Graph;Initial Catalog=化学类一班;Integrated Security=True;"/></connectionStrings> -----身份验证方式用的是windows身份验证,name属性是不可或缺的
新建一个html页面和一个一般处理程序,由于本次是将数据库中的数据一行一行打印在html上,肯定要用到循环,可以在html页面上写一个$tbody当做占位符
在一般处理程序中,首先要拿到web.config里面的连接字符串并建立数据库连接string constring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;using(SqlConnection conn=new SqlConnection(constring)){} //用using可以自动释放不在需要的对象的内存
然后建立SqlDataAdapter对象using (SqlDataAdapter adapter=new SqlDataAdapter ("select *from 学生登记表",conn)){} 因为SqlDataAdapter存在重载,可以将SQL语句当做参数写进去。
建立DataTable对象,DataTable的对象是一个虚拟表格,将从数据库中拿到的数据放在DataTable对象中DataTable da = new DataTable();
建立StringBuilder对象,他有一个AppendFormat方法可以比较方便在<td></td>中插入数据StringBuilder sb = new StringBuilder();
循环遍历 for (int i = 0; i < da.Rows.Count; i++) { sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", da.Rows[i]["autoId"], da.Rows[i]["学号"],
da.Rows[i]["姓名"]);}将对象da里面的每一行数据的分别拿到,用da.Rows[i]["列名"]可以拿到,然后放在td标签里面
最后要建立一般处理程序和html页面的连接string filePath = context.Request.MapPath("index.html");
string fileContent = File.ReadAllText(filePath);
fileContent= fileContent.Replace("$tbody", sb.ToString());//替换html页面中的$tbody占位符
context.Response.Write(fileContent);//打印
下面是源码:一班处理程序 indexList.ashx
<%@ WebHandler Language="C#" Class="indexList" %> using System; using System.Web; using System.IO; using System.Data; using System.Data.SqlClient; using System.Text; public class indexList : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; //context.Response.Write("Hello World"); string constring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection conn=new SqlConnection(constring)) { using (SqlDataAdapter adapter=new SqlDataAdapter ("select *from 学生登记表",conn)) { DataTable da = new DataTable(); adapter.Fill(da); StringBuilder sb = new StringBuilder(); for (int i = 0; i < da.Rows.Count; i++) { sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", da.Rows[i]["autoId"], da.Rows[i]["学号"], da.Rows[i]["姓名"]); } string filePath = context.Request.MapPath("index.html"); string fileContent = File.ReadAllText(filePath);//这是一般处理程序对html页面的读取 fileContent= fileContent.Replace("$tbody", sb.ToString()); context.Response.Write(fileContent); } } } public bool IsReusable { get { return false; } } }
web.config
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <connectionStrings> <add name="constr" connectionString="Data Source=Graph;Initial Catalog=化学类一班;Integrated Security=True;"/> </connectionStrings> </configuration>
index.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body><a href="#">111000</a> <table> <tr> <th>序号</th><th>学号</th><th>姓名</th> </tr> $tbody </table> </body> </html>
相关文章推荐
- ASP.NET 之 CheckBoxList在实际项目中的应用——高级查询、数据库读出默认选中
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- ASP_NET 2_0 监视你的应用---Trace----Error Handing---Web Event
- Asp.net中处理一个站点不同Web应用共享Session的问题
- Asp.net中如何处理一个站点不同Web应用通用Session的问题
- ASP.NET 例程完全代码版(5)——通过web.config配置数据库连接池
- Asp.Net查询MSSQL数据库的一个例子
- ASP.NET 2.0(C#)中System.Web.Caching的应用
- 轉載:ASP.NET 2.0实现数据库应用开发
- MVC模式在ASP.NET Web页面中的应用
- ASP.NET Web Page应用深入探讨
- 在ASP.NET WEB控件中应用样式(Style)
- ASP.NET应用中,Web表单之间的导航方式
- ASP.NET Web Page应用深入探讨
- 使数据库大字段的 Server Side ASP.Net Web/Http 下载,支持 FlashGet (Client) 断点续传多线程下载
- ASP.NET Web Page应用深入探讨
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- 简单的ASP.net查询数据库脚本
- ASP.NET 2.0如何实现数据库应用开发
- ASP.NET 2.0轻松实现数据库应用开发