自己写数据库访问ORM

 下面看一个例子:

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都网站建设、东源网络推广、成都小程序开发、东源网络营销、东源企业策划、东源品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供东源建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com

现在有一个用户信息的表:E-R图如下:

要实现该表的数据库新增、修改、查询功能,需要实现下面两个业务类:

 
 
 
 
  1. using Csla; 
  2. using IF.CslaCore; 
  3. using IF.OrmCore.DataSchema; 
  4. using System; 
  5. using System.Collections.Generic; 
  6. using System.ComponentModel; 
  7. using System.Linq; 
  8. using System.Text; 
  9. using System.Threading.Tasks; 
  10.  
  11. namespace IF.SysUser.Business 
  12.     [Serializable] 
  13.     [TableClass(FriendlyName="用户信息表",TableName="SYS_USER")] 
  14.     public class SysUser : IfBusiness 
  15.     { 
  16.         private static readonly PropertyInfo SUR_IDProperty = RegisterProperty(c => c.SUR_ID); 
  17.  
  18.         [DisplayName("SUR_ID")] 
  19.         [FieldDescription(IsPrimaryKey=true,ColumnName="SUR_ID",FriendlyName="SUR_ID",NeedUpdate=true)] 
  20.         public string SUR_ID { get; set; } 
  21.  
  22.         private static readonly PropertyInfo UserNameProperty = RegisterProperty(c => c.UserName); 
  23.         [DisplayName("登录名")] 
  24.         [FieldDescription(ColumnName="SUR_USERNAME",FriendlyName="登录名",NeedUpdate=true)] 
  25.         public string UserName { get; set; } 
  26.  
  27.  
  28.         private static readonly PropertyInfo NameProperty = RegisterProperty(c => c.Name); 
  29.         [DisplayName("姓名")] 
  30.         [FieldDescription(ColumnName="SUR_NAME",FriendlyName="姓名",NeedUpdate=true)] 
  31.         public string Name { get; set; } 
  32.  
  33.         private static readonly PropertyInfo PasswordProperty = RegisterProperty(c => c.Password); 
  34.         [DisplayName("密码")] 
  35.         [FieldDescription(ColumnName="SUR_PASSWORD",FriendlyName="密码",NeedUpdate=true)] 
  36.         public string Password { get; set; } 
  37.  
  38.         private static readonly PropertyInfo LoginMacProperty = RegisterProperty(c => c.LoginMac); 
  39.         [DisplayName("登录Mac地址")] 
  40.         [FieldDescription(ColumnName="SUR_LOGIN_MAC",FriendlyName="登录Mac地址",NeedUpdate=true)] 
  41.         public string LoginMac { get; set; } 
  42.  
  43.         private static readonly PropertyInfo LoginIPProperty = RegisterProperty(c => c.LoginIP); 
  44.         [DisplayName("登录IP")] 
  45.         [FieldDescription(ColumnName="SUR_LOGIN_IP",FriendlyName="登录IP",NeedUpdate=true)] 
  46.         public string LoginIP { get; set; } 
  47.  
  48.  
  49.         private static readonly PropertyInfo LoginTimeProperty = RegisterProperty(c => c.LoginTime); 
  50.         [DisplayName("登录时间")] 
  51.         [FieldDescription(ColumnName="SUR_LOGIN_TIME",FriendlyName="登录时间",NeedUpdate=true)] 
  52.         public DateTime? LoginTime { get; set; } 
  53.  
  54.         private static readonly PropertyInfo LogoutTimeProperty = RegisterProperty(c => c.LogoutTime); 
  55.         [DisplayName("登出时间")] 
  56.         [FieldDescription(ColumnName="SUR_LOGOUT_TIME",FriendlyName="登出时间",NeedUpdate=true)] 
  57.         public DateTime? LogoutTime { get; set; } 
  58.  
  59.         private static readonly PropertyInfo LoginFailTimeProperty = RegisterProperty(c => c.LoginFailTime); 
  60.         [DisplayName("登录失败时间")] 
  61.         [FieldDescription(ColumnName="SUR_LOGIN_FAIL_TIME",FriendlyName="登录失败时间",NeedUpdate=true)] 
  62.         public DateTime? LoginFailTime { get; set; } 
  63.  
  64.         private static readonly PropertyInfo LoginFailCountProperty = RegisterProperty(c => c.LoginFailCount); 
  65.         [DisplayName("登录失败次数")] 
  66.         [FieldDescription(ColumnName="SUR_LOGIN_FAIL_COUNT",FriendlyName="登录失败次数",NeedUpdate=true)] 
  67.         public Int32? LoginFailCount { get; set; } 
  68.  
  69.  
  70.         private static readonly PropertyInfo LockFGProperty = RegisterProperty(c => c.LockFG); 
  71.         [DisplayName("是否锁定")] 
  72.         [FieldDescription(ColumnName="SUR_LOCK_FG",FriendlyName="是否锁定",NeedUpdate=true)] 
  73.         public bool? LockFG { get; set; } 
  74.  
  75.         private static readonly PropertyInfo DisableFGProperty = RegisterProperty(c => c.DisableFG); 
  76.         [DisplayName("是否有效")] 
  77.         [FieldDescription(ColumnName="SUR_DISABLE_FG",FriendlyName="是否有效",NeedUpdate=true)] 
  78.         public bool? DisableFG { get; set; } 
  79.  
  80.  
  81.         #region 通用字段 
  82.  
  83.         private static readonly PropertyInfo CreateTimeProperty = RegisterProperty(c => c.CreateTime); 
  84.         [DisplayName("创建时间")] 
  85.         [FieldDescription(ColumnName="CreateTime",FriendlyName="创建时间",NeedUpdate=true)] 
  86.         public override DateTime? CreateTime { get; set; } 
  87.  
  88.         private static readonly PropertyInfo LastUpdateTimeProperty = RegisterProperty(c => c.LastUpdateTime); 
  89.         [DisplayName("***修改时间")] 
  90.         [FieldDescription(ColumnName="LastUpdateTime",FriendlyName="***修改时间",NeedUpdate=true)] 
  91.         public override DateTime? LastUpdateTime { get; set; } 
  92.  
  93.  
  94.  
  95.         public override void SetPrimaryKey(string key) 
  96.         { 
  97.             SUR_ID = key; 
  98.         } 
  99.         #endregion 
  100.     } 
  101.  
  102.     [Serializable] 
  103.     public class SysUserList : IfBusinessList 
  104.     { } 

现在就可以工作了:

全表检索数据方法:

 
 
 
 
  1. SysUserList selData = SysUserList.Fetch(); 

向数据库新增一条数据:

 
 
 
 
  1. SysUser.Business.SysUser user = new SysUser.Business.SysUser(); 
  2. user.UserName= "inaction"; 
  3. user.Name = "流砂"; 
  4. user.Password= "superman"; 
  5. selData.Add(user); 
  6. selData.Save(); 

向数据库修改数据:

 
 
 
 
  1. var user = SysUserList.Fetch(c => c.UserName == "inaction"); 
  2. user.Password = "123456"; 
  3.  SysUserList list = new SysUserList { user }; 
  4.  list.Save(); 

以上代码就实现了对密码的修改。

特别说明:目前IF 只能通过SysUserList对象的Save方法保存数据。以后会实现通过业务类自身Save方法保存数据。

新闻名称:自己写数据库访问ORM
本文URL:http://www.hantingmc.com/qtweb/news21/511471.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联