数据库字段映射到类实体

香水坏坏 发表于 2007-7-7 [ASP.NET]

 

作者: 香水坏坏

转载请著名,谢谢!

 

C#代码
  1. using System;       
  2. using System.Data;       
  3. using System.Collections.Generic;       
  4. using System.Text;       
  5. using System.Reflection;     

 

C#代码
  1. namespace YourNamespace   
  2. {   
  3.     public abstract class BaseModule   
  4.     {   
  5.         private long _id;   
  6.   
  7.         [MSSQLField("id")]   
  8.         public long UniqueID   
  9.         {   
  10.             get { return _id; }   
  11.             set { _id = value; }   
  12.         }   
  13.   
  14.         public BaseModule(){}   
  15.   
  16.         public BaseModule(IDataReader dr)   
  17.         {           
  18.             Type t = this.GetType();   
  19.             PropertyInfo[] properties  = t.GetProperties();   
  20.               
  21.             for (int i = 0; i < dr.FieldCount; i++)   
  22.             {   
  23.                 string fieldname = dr.GetName(i).ToLower();   
  24.                 object fieldvalue = dr.GetValue(i);   
  25.                 if (fieldvalue != DBNull.Value)   
  26.                 {   
  27.                     for (int j = 0; j < properties.Length; j++)   
  28.                     {   
  29.                         if (BindDataReaderValue(this, properties[j], fieldvalue, fieldname))   
  30.                             break;   
  31.                     }   
  32.                 }   
  33.             }   
  34.         }   
  35.   
  36.         private bool BindDataReaderValue(object des, PropertyInfo property, object val , string tbField)   
  37.         {   
  38.             MSSQLFieldAttribute[] atts = (MSSQLFieldAttribute[])property.GetCustomAttributes(typeof(MSSQLFieldAttribute), true);   
  39.             if (property.CanWrite && atts.Length > 0)   
  40.             {   
  41.                 if (atts[0].IsComplexClass == false)   
  42.                 {   
  43.                     if (atts[0].FieldName == tbField)   
  44.                     {   
  45.                         if (property.PropertyType.IsEnum)   
  46.                         {   
  47.                             property.SetValue(des, Enum.ToObject(property.PropertyType, val), null);   
  48.                         }   
  49.                         else  
  50.                         {   
  51.                             property.SetValue(des, val, null);   
  52.                         }   
  53.                         return true;   
  54.                     }   
  55.                 }   
  56.                 else  
  57.                 {   
  58.                     object complexObject = property.GetValue( this , null );   
  59.                     if (null == complexObject)   
  60.                     {   
  61.                         complexObject = property.PropertyType.GetConstructor(System.Type.EmptyTypes).Invoke(null);   
  62.                         property.SetValue(this, complexObject, null);   
  63.                     }   
  64.                     return BindDataReaderClass(complexObject, val, tbField);   
  65.                 }   
  66.             }   
  67.             return false;   
  68.         }   
  69.   
  70.         private bool BindDataReaderClass(object des, object val, string tbField)   
  71.         {   
  72.             PropertyInfo[] properties = des.GetType().GetProperties();   
  73.             for (int i = 0; i < properties.Length; i++)   
  74.             {   
  75.                 if (BindDataReaderValue(des, properties[i], val, tbField))   
  76.                     return true;   
  77.             }   
  78.             return false;   
  79.         }   
  80.     }   
  81.   
  82.     [AttributeUsage( AttributeTargets.Property , AllowMultiple=false ) ]   
  83.     public class MSSQLFieldAttribute : Attribute   
  84.     {   
  85.         private string _fieldname;   
  86.         private bool _isComplexClass;   
  87.   
  88.         public bool IsComplexClass   
  89.         {   
  90.             get { return _isComplexClass; }   
  91.             set { _isComplexClass = value; }   
  92.         }   
  93.         public string FieldName   
  94.         {   
  95.             get { return _fieldname; }   
  96.             set { _fieldname = value.ToLower(); }   
  97.         }   
  98.   
  99.         public MSSQLFieldAttribute() { _isComplexClass = true; }   
  100.         public MSSQLFieldAttribute(string fieldname)   
  101.         {   
  102.             _fieldname = fieldname;   
  103.             
  104.         }   
  105.       
  106.     }   
  107. }   
  108.   
  109.   
514 0 标签:C# 数据库 
访客评论
    发表评论
    • 你的姓名:
    • 你的网站:
    •   EMAIL:   
    • 评论内容:
    • 私人