柔城

SOSOFT articles

C#利用反射+特性实现简单的实体映射数据库操作类

附上源代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 反射_特性
 9 {
10     [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
11     public class FieldAttribute : Attribute
12     {
13         private string _Fields;
14         /// <summary>
15         /// 字段名称 keleyi.com
16         /// </summary>
17         public string Fields
18         {
19             get { return _Fields; }
20 
21         }
22 
23         private DbType _Dbtype;
24         /// <summary>
25         /// 字段类型
26         /// </summary>
27         public DbType Dbtype
28         {
29             get { return _Dbtype; }
30 
31         }
32 
33         private int _ValueLength;
34         /// <summary>
35         /// 字段值长度
36         /// </summary>
37         public int ValueLength
38         {
39             get { return _ValueLength; }
40 
41         }
42         /// <summary>
43         /// 构造函数
44         /// </summary>
45         /// <param name="fields"> 字段名</param>
46         /// <param name="types"> 字段类型</param>
47         /// <param name="i"> 字段值长度</param>
48         public FieldAttribute(string fields, DbType types, int i)
49         {
50 
51             _Fields = fields;
52             _Dbtype = types;
53             _ValueLength = i;
54         }
55     }
56 }

2:表名特性

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 反射_特性
 8 {
 9     [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
10     public class TableAttribute : Attribute
11     {
12         private string _TableName;
13         /// <summary>
14         /// 映射的表名
15         /// </summary>
16         public string TableName
17         {
18             get { return _TableName; }
19         }
20         /// <summary>
21         /// 定位函数映射表名;
22         /// </summary>
23         /// <param name="table"></param>
24         public TableAttribute(string table)
25         {
26             _TableName = table;
27         }
28     }
29 }

3:特性测试类

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace 特性_反射
  9 {
 10     [Table("Consumers")]
 11     public class UserInf
 12     {
 13         private string _UserID;
 14         /// <summary>
 15         /// 登陆ID
 16         /// </summary>
 17         [Field("ConsumerID", DbType.String, 12)]
 18         public string U_UserID
 19         {
 20             get { return _UserID; }
 21             set { _UserID = value; }
 22         }
 23 
 24         private string _Psw;
 25         /// <summary>
 26         /// 登陆密码
 27         /// </summary>
 28         [Field("ConsumerPwd", DbType.String, 12)]
 29         public string U_Psw
 30         {
 31             get { return _Psw; }
 32             set { _Psw = value; }
 33         }
 34 
 35         private string _UserName;
 36         /// <summary>
 37         /// 用户别称
 38         /// </summary>
 39         [Field("ConsumerName", DbType.String, 50)]
 40         public string U_UserName
 41         {
 42             get { return _UserName; }
 43             set { _UserName = value; }
 44         }
 45 
 46         private string _City;
 47         /// <summary>
 48         /// 所住城市
 49         /// </summary>
 50         [Field("UserCity", DbType.String, 50)]
 51         public string U_City
 52         {
 53             get { return _City; }
 54             set { _City = value; }
 55         }
 56 
 57         private int _Popedom;
 58         /// <summary>
 59         /// 权限
 60         /// </summary>
 61         [Field("popedom", DbType.Int32, 0)]
 62         public int U_Popedom
 63         {
 64             get { return _Popedom; }
 65             set { _Popedom = value; }
 66         }
 67 
 68         private DateTime _AddDataTime;
 69         /// <summary>
 70         /// 注册时间
 71         /// </summary>
 72         [Field("addDataTime", DbType.Date, 0)]
 73         public DateTime U_AddDataTime
 74         {
 75             get { return _AddDataTime; }
 76             set { _AddDataTime = value; }
 77         }
 78 
 79         private int _Sex;
 80         /// <summary>
 81         /// 性别
 82         /// </summary>
 83         [Field("Sex", DbType.Int32, 0)]
 84         public int U_Sex
 85         {
 86             get { return _Sex; }
 87             set { _Sex = value; }
 88         }
 89 
 90         private int _BirthTime;
 91         /// <summary>
 92         /// 出身日期;
 93         /// </summary>
 94         [Field("BirthTime", DbType.String, 9)]
 95         public int U_BirthTime
 96         {
 97             get { return _BirthTime; }
 98             set { _BirthTime = value; }
 99         }
100     }
101 }

4:测试控制台程序

                UserInf userss = new UserInf();
                userss.U_UserID = "aw12311";
                userss.U_Psw = "123";
                userss.U_UserName = "aw";
                userss.U_City = "武汉";
                userss.U_Popedom = 1;
                userss.U_Sex = 1;
                userss.U_BirthTime = 19900114;
                userss.U_AddDataTime = DateTime.Now;

                DateIsTableAttribute<UserInf> t = new DateIsTableAttribute<UserInf>();

                Response.Write(" </br>" + t.insertDate(userss));

http://www.cnblogs.com/sosoft/

posted on 2013-12-06 16:38  柔城  阅读(2727)  评论(3编辑  收藏  举报

导航