《ASP.net组件设计》PowerORM的完整代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
public partial class PowerORM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strConnection = "Server=127.0.0.1;User ID=sa;Password=qqww;Persist Security Info=True;DataBase=Northwind;";
string strSQL = "SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]";
IList myList;
using (SqlDataReader myReader = SqlHelper.ExecuteReader(strConnection, CommandType.Text, strSQL))
{
myList = myPowerORM.FillHelper.Fill((new myPowerORM.Customer()).GetType(), myReader);
}
Response.Write("<pre>");
foreach (object o in myList)
{
myPowerORM.Customer c = (myPowerORM.Customer)o;
Response.Write(c.CustomerID+"\t"+c.CompanyName+"\t"+c.ContactNameI+"\n<br>");
}
Response.Write("</pre>");
}
}
namespace myPowerORM
{
public class Customer
{
private string _customerID, _companyName, _contactName;
[Column("CustomerID")]
public string CustomerID
{
get { return _customerID; }
set { _customerID = value; }
}
[Column("CompanyName")]
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; }
}
[Column("ContactName")]
public string ContactNameI
{
get { return _contactName; }
set { _contactName = value; }
}
public Customer() { }
}
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class ColumnAttribute : Attribute
{
private string _columnName = null;
public string ColumnName
{
get { return _columnName; }
}
public ColumnAttribute(string columnName)
{
_columnName = columnName;
}
}
public class FillHelper
{
public static IList Fill(Type rowType, IDataReader reader)
{
ArrayList dataList = new ArrayList();
while (reader.Read())
{
object item = Activator.CreateInstance(rowType, false); //使用与指定参数匹配程度最高的构造函数创建指定类型的实例
foreach (MemberInfo mi in rowType.GetMembers())
{
foreach (ColumnAttribute attr in mi.GetCustomAttributes(typeof(ColumnAttribute), false))
{
int index = reader.GetOrdinal(attr.ColumnName); //在给定列名称的情况下获取列序号
if (index != -1)
{
if (mi.MemberType == MemberTypes.Field)
((FieldInfo)mi).SetValue(item, reader.GetValue(index));
else if (mi.MemberType == MemberTypes.Property)
((PropertyInfo)mi).SetValue(item, reader.GetValue(index), null);
}
}
}
dataList.Add(item);
}
return dataList;
}
public FillHelper() { }
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
public partial class PowerORM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strConnection = "Server=127.0.0.1;User ID=sa;Password=qqww;Persist Security Info=True;DataBase=Northwind;";
string strSQL = "SELECT [CustomerID], [CompanyName], [ContactName] FROM [Customers]";
IList myList;
using (SqlDataReader myReader = SqlHelper.ExecuteReader(strConnection, CommandType.Text, strSQL))
{
myList = myPowerORM.FillHelper.Fill((new myPowerORM.Customer()).GetType(), myReader);
}
Response.Write("<pre>");
foreach (object o in myList)
{
myPowerORM.Customer c = (myPowerORM.Customer)o;
Response.Write(c.CustomerID+"\t"+c.CompanyName+"\t"+c.ContactNameI+"\n<br>");
}
Response.Write("</pre>");
}
}
namespace myPowerORM
{
public class Customer
{
private string _customerID, _companyName, _contactName;
[Column("CustomerID")]
public string CustomerID
{
get { return _customerID; }
set { _customerID = value; }
}
[Column("CompanyName")]
public string CompanyName
{
get { return _companyName; }
set { _companyName = value; }
}
[Column("ContactName")]
public string ContactNameI
{
get { return _contactName; }
set { _contactName = value; }
}
public Customer() { }
}
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public class ColumnAttribute : Attribute
{
private string _columnName = null;
public string ColumnName
{
get { return _columnName; }
}
public ColumnAttribute(string columnName)
{
_columnName = columnName;
}
}
public class FillHelper
{
public static IList Fill(Type rowType, IDataReader reader)
{
ArrayList dataList = new ArrayList();
while (reader.Read())
{
object item = Activator.CreateInstance(rowType, false); //使用与指定参数匹配程度最高的构造函数创建指定类型的实例
foreach (MemberInfo mi in rowType.GetMembers())
{
foreach (ColumnAttribute attr in mi.GetCustomAttributes(typeof(ColumnAttribute), false))
{
int index = reader.GetOrdinal(attr.ColumnName); //在给定列名称的情况下获取列序号
if (index != -1)
{
if (mi.MemberType == MemberTypes.Field)
((FieldInfo)mi).SetValue(item, reader.GetValue(index));
else if (mi.MemberType == MemberTypes.Property)
((PropertyInfo)mi).SetValue(item, reader.GetValue(index), null);
}
}
}
dataList.Add(item);
}
return dataList;
}
public FillHelper() { }
}
}
posted on 2005-07-12 09:50 Goodspeed 阅读(1521) 评论(0) 编辑 收藏 举报