架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 15161|回复: 0

[ASP.NET] DataTable转换为List<Model>的通用类示例

[复制链接]
发表于 2015-4-22 09:09:58 | 显示全部楼层 |阅读模式
[DBHelp]利用反射技术封装对象到List
http://www.itsvse.com/thread-1848-1-1.html
(出处: 武汉软件工程职业学院)
原文链接:

DBhelp.cs代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.SqlClient;
  6. using System.Reflection;
  7. using System.Data;

  8. namespace TestList
  9. {
  10.     public class DBhelp
  11.     {
  12.         private static string strConn = "server=.;database=testlist;integrated security=true";

  13.         //得到一个DataTable
  14.         public static DataTable getTable(string strSql)
  15.         {
  16.             SqlConnection sqlConn = new SqlConnection(strConn);
  17.             SqlDataAdapter sqlDA = new SqlDataAdapter(strSql, sqlConn);
  18.             DataTable dt = new DataTable();
  19.             sqlDA.Fill(dt);
  20.             return dt;
  21.         }

  22.         //增删改
  23.         public static void upDateDB(string strSql)
  24.         {
  25.             SqlConnection sqlConn = new SqlConnection(strConn);
  26.             SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
  27.             sqlConn.Open();
  28.             sqlComm.ExecuteNonQuery();
  29.             sqlConn.Close();
  30.         }


  31.         //得到一个list<T>
  32.         public static List<T> getList<T>(string strSql)
  33.         {
  34.             //委托的实例化,指向packBean方法  
  35.             //PackageBean packageBean = new PackageBean(packBean);
  36.             List<T> list = new List<T>();

  37.             SqlConnection sqlConn = new SqlConnection(strConn);
  38.             SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
  39.             sqlConn.Open();
  40.             SqlDataReader sqlDR = sqlComm.ExecuteReader();

  41.             while (sqlDR.Read())
  42.             {
  43.                 //得到T的类型
  44.                 Type t = typeof(T); ;

  45.                 //查看类中的属性:
  46.                 PropertyInfo[] pis = t.GetProperties();
  47.                 // 用反射生成对象
  48.                 T model = Activator.CreateInstance<T>();
  49.                 foreach (PropertyInfo pi in pis)
  50.                 {
  51.                     if (pi != null)
  52.                     {
  53.                         //取得特定字段并赋值
  54.                         pi.SetValue(model, sqlDR[pi.Name].ToString(), null);
  55.                     }
  56.                 }

  57.                 list.Add(model);
  58.             }
  59.             sqlConn.Close();

  60.             return list;

  61.         }
  62.     }
  63. }
复制代码
stuinfo.cs代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;

  5. namespace TestList
  6. {
  7.     public class stuinfo
  8.     {
  9.         public string id { get; set; }
  10.         public string name { get; set; }
  11.         public string age { get; set; }
  12.         public string live { get; set; }

  13.     }
  14. }
复制代码
test.asp代码:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="TestList.test" %>

  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title></title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
  11.             <Columns>
  12.                 <asp:BoundField DataField="id" HeaderText="id" />
  13.                 <asp:BoundField DataField="name" HeaderText="name" />
  14.                 <asp:BoundField DataField="age" HeaderText="age" />
  15.                 <asp:BoundField DataField="live" HeaderText="live" />
  16.             </Columns>
  17.         </asp:GridView>
  18.     </div>
  19.     </form>
  20. </body>
  21. </html>
复制代码
test.asp.cs代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;

  7. namespace TestList
  8. {
  9.     public partial class test : System.Web.UI.Page
  10.     {
  11.         protected void Page_Load(object sender, EventArgs e)
  12.         {
  13.             if (!IsPostBack) {
  14.                 List<stuinfo> stu=DBhelp.getList<stuinfo>("select * from stuinfo");

  15.                 GridView1.DataSource = stu;
  16.                 GridView1.DataBind();
  17.             }
  18.         }
  19.     }
  20. }
复制代码
sql数据库:
  1. create database testlist
  2. go
  3. use testlist
  4. go

  5. create table stuinfo
  6. (
  7.         id int primary key identity(1,1),
  8.         name varchar(5) not null,
  9.         age int not null,
  10.         live varchar(20) not null
  11. )
  12. go

  13. insert into stuinfo values('东方','45','地方')
  14. insert into stuinfo values('当初','47','发多个地方')
  15. insert into stuinfo values('并不','2','的方式的水电费')
  16. insert into stuinfo values('学校','11','的师傅的说法')
  17. insert into stuinfo values('形成','87','的释放速度')
  18. insert into stuinfo values('自行','45','士大夫似')

  19. select * from stuinfo
复制代码


压缩包下载:
TestList.rar (26.89 KB, 下载次数: 0, 售价: 1 粒MB)




上一篇:职业素养《职业生涯规划书模板》
下一篇:桂林老兵cookie欺骗工具
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-7-2 04:31

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表