asp.net三层架构中使用泛型获取实体数据
2011-10-25 14:12
369 查看
asp.net中使用泛型获取实体数据可以发挥更高的效率,代码简洁方便,本例采用三层架构。首先在model层中定义StuInfo实体,然后在DAL层的SQLHelper数据操作类中定义list<>泛型查询数据库获取实体数据,最后通过BLL层的方法调用出来。具体实例如下:
一、model层中定义的StuInfo实体:
二、DAL层中通过list泛型获取实体数据:
三、在BLL层中调用DAL层的方法获取实体数据:
四、在web UI层中显示实体数据
经过第三步,实体数据已经添加到list泛型集合中了,那么如何显示出来呢?可以在页面上添加数据显示控件,如GridView,然后在cs代码中给它指定一个数据源,这个list泛型集合就可以作为它的数据源,让它显示数据。
本例经过作者亲自调试通过。
(本文转自小谈博客:http://tanteng.sinaapp.com/2011/10/ilist/,转载请注明出处!)
一、model层中定义的StuInfo实体:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Model { public class StuInfo { private string _id; public string Id { get { return _id; } set { _id = value; } } private string _stuname; public string Stuname { get { return _stuname; } set { _stuname = value; } } private string _stuclass; public string Stuclass { get { return _stuclass; } set { _stuclass = value; } } } }
二、DAL层中通过list泛型获取实体数据:
public List<StuInfo> date() { List<StuInfo> list = null; cmd = new SqlCommand("select * from stuinfo", conn); try { conn.Open(); dr = cmd.ExecuteReader(); list = new List<StuInfo>(); while (dr.Read()) { StuInfo stu = new StuInfo(); stu.Id = dr["id"].ToString(); stu.Stuname = dr["stuname"].ToString(); stu.Stuclass = dr["stuclass"].ToString(); list.Add(stu); } return list; } catch (Exception) { return null; } finally { conn.Close(); } }
三、在BLL层中调用DAL层的方法获取实体数据:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DAL; using Model; namespace BLL { public class StuDel { SQLHelper db = new SQLHelper(); public List<StuInfo> date() { return db.date(); } } }
四、在web UI层中显示实体数据
经过第三步,实体数据已经添加到list泛型集合中了,那么如何显示出来呢?可以在页面上添加数据显示控件,如GridView,然后在cs代码中给它指定一个数据源,这个list泛型集合就可以作为它的数据源,让它显示数据。
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using BLL; public partial class _Default : System.Web.UI.Page { StuDel de = new StuDel(); protected void Page_Load(object sender, EventArgs e) { } protected void Button2_Click(object sender, EventArgs e) { GridView1.DataSource = de.date(); GridView1.DataBind(); } }
本例经过作者亲自调试通过。
(本文转自小谈博客:http://tanteng.sinaapp.com/2011/10/ilist/,转载请注明出处!)
相关文章推荐
- 在ASP.NET三层架构中使用泛型获取实体数据
- asp.net中,我们使用ashx获取数据列表,在前端使用$.ajax()解析
- 三层架构中使用实体类填充泛型集合代替DataTable解决方案(ASP.NET+C#)
- ASP.NET 跳转页面数据的获取 (使用html控件)
- Asp.Net使用代理IP远程获取数据
- ASP.NET中通过WebService获取数据来使用Highcharts图表控件
- asp.net中,我们使用ashx获取数据列表,在前端使用$.ajax()解析
- ASP.NET中动态获取数据使用Highcharts图表控件【Copy By Internet】
- ASP.NET Web API实践系列07,获取数据, 使用Ninject实现依赖倒置,使用Knockout实现页面元素和视图模型的双向绑定
- ASP.NET中通过WebService获取数据来使用Highcharts图表控件
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- ASP.NET中动态获取数据使用Highcharts图表控件
- ztree 使用asp.net mvc异步动态获取数据
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- 爬虫如何抓取到Asp.Net中-doPostBack获取新页面的数据
- 关于JQuery 中$.ajax函数利用jsonp实现Ajax跨域请求ASP.NET的WebService成功获取数据的案例
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- ASP.NET系列之点滴积累(一):GridView中获取选定行中数据