原文链接:
DBhelp.cs代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data.SqlClient;
- using System.Reflection;
- using System.Data;
- namespace TestList
- {
- public class DBhelp
- {
- private static string strConn = "server=.;database=testlist;integrated security=true";
- //得到一个DataTable
- public static DataTable getTable(string strSql)
- {
- SqlConnection sqlConn = new SqlConnection(strConn);
- SqlDataAdapter sqlDA = new SqlDataAdapter(strSql, sqlConn);
- DataTable dt = new DataTable();
- sqlDA.Fill(dt);
- return dt;
- }
- //增删改
- public static void upDateDB(string strSql)
- {
- SqlConnection sqlConn = new SqlConnection(strConn);
- SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
- sqlConn.Open();
- sqlComm.ExecuteNonQuery();
- sqlConn.Close();
- }
- //得到一个list<T>
- public static List<T> getList<T>(string strSql)
- {
- //委托的实例化,指向packBean方法
- //PackageBean packageBean = new PackageBean(packBean);
- List<T> list = new List<T>();
- SqlConnection sqlConn = new SqlConnection(strConn);
- SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
- sqlConn.Open();
- SqlDataReader sqlDR = sqlComm.ExecuteReader();
- while (sqlDR.Read())
- {
- //得到T的类型
- Type t = typeof(T); ;
- //查看类中的属性:
- PropertyInfo[] pis = t.GetProperties();
- // 用反射生成对象
- T model = Activator.CreateInstance<T>();
- foreach (PropertyInfo pi in pis)
- {
- if (pi != null)
- {
- //取得特定字段并赋值
- pi.SetValue(model, sqlDR[pi.Name].ToString(), null);
- }
- }
- list.Add(model);
- }
- sqlConn.Close();
- return list;
- }
- }
- }
复制代码 stuinfo.cs代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace TestList
- {
- public class stuinfo
- {
- public string id { get; set; }
- public string name { get; set; }
- public string age { get; set; }
- public string live { get; set; }
- }
- }
复制代码 test.asp代码:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="TestList.test" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundField DataField="id" HeaderText="id" />
- <asp:BoundField DataField="name" HeaderText="name" />
- <asp:BoundField DataField="age" HeaderText="age" />
- <asp:BoundField DataField="live" HeaderText="live" />
- </Columns>
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
复制代码 test.asp.cs代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace TestList
- {
- public partial class test : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack) {
- List<stuinfo> stu=DBhelp.getList<stuinfo>("select * from stuinfo");
- GridView1.DataSource = stu;
- GridView1.DataBind();
- }
- }
- }
- }
复制代码 sql数据库:
- create database testlist
- go
- use testlist
- go
- create table stuinfo
- (
- id int primary key identity(1,1),
- name varchar(5) not null,
- age int not null,
- live varchar(20) not null
- )
- go
- insert into stuinfo values('东方','45','地方')
- insert into stuinfo values('当初','47','发多个地方')
- insert into stuinfo values('并不','2','的方式的水电费')
- insert into stuinfo values('学校','11','的师傅的说法')
- insert into stuinfo values('形成','87','的释放速度')
- insert into stuinfo values('自行','45','士大夫似')
- select * from stuinfo
复制代码
压缩包下载:
TestList.rar
(26.89 KB, 下载次数: 0, 售价: 1 粒MB)
|