• 《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() { }
        }

    }
  • 相关阅读:
    jQuery常用操作
    SharePoint2007深入浅出——使用jQuery UI
    深入浅出SharePoint——常用的url命令
    MySQL实战
    Emacs助力PowerShell
    电商平台实战——准备篇
    深入浅出SharePoint2012——安装Report Service
    (转).net面试题(老赵)
    (转)在.NET程序运行过程中,什么是堆,什么是栈?什么情况下会在堆(栈)上分配数据?它们有性能上的区别吗?“结构”对象可能分配在堆上吗?什么情况下会发生,有什么需要注意的吗?
    (转)类(class)和结构(struct)的区别是什么?它们对性能有影响吗?.NET BCL里有哪些是类(结构),为什么它们不是结构(类)?在自定义类型时,您如何选择是类还是结构?
  • 原文地址:https://www.cnblogs.com/goodspeed/p/191141.html
Copyright © 2020-2023  润新知