简单的ORM

自定义属性:
ContractedBlock.gifExpandedBlockStart.gif自定义属性
 1None.gifusing System;
 2None.gifusing System.ComponentModel;
 3None.gif
 4None.gifnamespace Relaction.ORM
 5ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// 
 8InBlock.gif    /// </summary>
 9ExpandedSubBlockEnd.gif    /// 

10InBlock.gif    [AttributeUsage(AttributeTargets.Property,Inherited = false,AllowMultiple = false)]
11InBlock.gif    public class ColumnAttribute:Attribute
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        private string _columnName = null;
14InBlock.gif        public string ColumnName
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            get
17ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
18InBlock.gif                return _columnName;
19ExpandedSubBlockEnd.gif            }

20ExpandedSubBlockEnd.gif        }

21InBlock.gif
22InBlock.gif        public ColumnAttribute(string columnName)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            _columnName = columnName;
25ExpandedSubBlockEnd.gif        }

26ExpandedSubBlockEnd.gif    }

27ExpandedBlockEnd.gif}

28None.gif

实体类:
ContractedBlock.gifExpandedBlockStart.gif实体类,并添加上相应属性
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Relaction.ORM
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// 
 7ExpandedSubBlockEnd.gif    /// </summary>

 8InBlock.gif    public class Customer
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        private string _customerID,_companyName,_contactName;
11InBlock.gif        [Column("CustomerID")]
12InBlock.gif        public string CustomerID
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            get
15ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
16InBlock.gif                return _customerID;
17ExpandedSubBlockEnd.gif            }

18InBlock.gif            set
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                _customerID = value;
21ExpandedSubBlockEnd.gif            }

22InBlock.gif
23ExpandedSubBlockEnd.gif        }

24InBlock.gif        [Column("CompanyName")]
25InBlock.gif        public string CompanyName
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            get
28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
29InBlock.gif                return _companyName;
30ExpandedSubBlockEnd.gif            }

31InBlock.gif            set
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                _companyName = value;
34ExpandedSubBlockEnd.gif            }

35ExpandedSubBlockEnd.gif        }

36InBlock.gif        [Column("ContactName")]
37InBlock.gif        public string ContactName
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            get
40ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
41InBlock.gif                return _contactName;
42ExpandedSubBlockEnd.gif            }

43InBlock.gif            set
44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
45InBlock.gif                _contactName = value;
46ExpandedSubBlockEnd.gif            }

47ExpandedSubBlockEnd.gif        }

48InBlock.gif        public Customer()
49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
50ExpandedSubBlockEnd.gif        }

51ExpandedSubBlockEnd.gif    }

52ExpandedBlockEnd.gif}

53None.gif

简单的ORM Helper
ContractedBlock.gifExpandedBlockStart.gifORM Helper
 1None.gifusing System;
 2None.gif
 3None.gifusing System.Collections;
 4None.gifusing System.ComponentModel;
 5None.gifusing System.Reflection;
 6None.gifusing System.Data;
 7None.gifusing System.Data.Common;
 8None.gif
 9None.gifnamespace Relaction.ORM
10ExpandedBlockStart.gifContractedBlock.gifdot.gif{
11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
12InBlock.gif    /// 
13ExpandedSubBlockEnd.gif    /// </summary>

14InBlock.gif    public class FillHelper
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16InBlock.gif        public static IList Fill(Type rowType,IDataReader reader)
17ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
18InBlock.gif            ArrayList dataList = new ArrayList();
19InBlock.gif            while(reader.Read())
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                object item = Activator.CreateInstance(rowType,false);
22InBlock.gif                foreach(MemberInfo mi in rowType.GetMembers())
23ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
24InBlock.gif                    foreach(ColumnAttribute attr in mi.GetCustomAttributes(typeof(ColumnAttribute),false))
25ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
26InBlock.gif                        int index = reader.GetOrdinal(attr.ColumnName);
27InBlock.gif                        if(index != -1 )
28ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
29InBlock.gif                            if(mi.MemberType == MemberTypes.Field)
30InBlock.gif                                ((FieldInfo)mi).SetValue(item,reader.GetValue(index));
31InBlock.gif                            else
32InBlock.gif                                if(mi.MemberType == MemberTypes.Property)
33InBlock.gif                                    ((PropertyInfo)mi).SetValue(item,reader.GetValue(index),null);
34ExpandedSubBlockEnd.gif                        }

35ExpandedSubBlockEnd.gif                    }

36ExpandedSubBlockEnd.gif                }
 
37InBlock.gif                dataList.Add(item);
38ExpandedSubBlockEnd.gif            }

39InBlock.gif            return dataList;
40ExpandedSubBlockEnd.gif        }

41InBlock.gif        public FillHelper()
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43ExpandedSubBlockEnd.gif        }

44ExpandedSubBlockEnd.gif    }

45ExpandedBlockEnd.gif}

46None.gif

转载于:https://www.cnblogs.com/nanshouyong326/archive/2006/11/28/574842.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值