C#利用反射+特性实现简单的实体映射数据库操作类
2024-10-16 09:18:17
附上源代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 反射_特性
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class FieldAttribute : Attribute
{
private string _Fields;
/// <summary>
/// 字段名称 keleyi.com
/// </summary>
public string Fields
{
get { return _Fields; } } private DbType _Dbtype;
/// <summary>
/// 字段类型
/// </summary>
public DbType Dbtype
{
get { return _Dbtype; } } private int _ValueLength;
/// <summary>
/// 字段值长度
/// </summary>
public int ValueLength
{
get { return _ValueLength; } }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="fields"> 字段名</param>
/// <param name="types"> 字段类型</param>
/// <param name="i"> 字段值长度</param>
public FieldAttribute(string fields, DbType types, int i)
{ _Fields = fields;
_Dbtype = types;
_ValueLength = i;
}
}
}
2:表名特性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 反射_特性
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class TableAttribute : Attribute
{
private string _TableName;
/// <summary>
/// 映射的表名
/// </summary>
public string TableName
{
get { return _TableName; }
}
/// <summary>
/// 定位函数映射表名;
/// </summary>
/// <param name="table"></param>
public TableAttribute(string table)
{
_TableName = table;
}
}
}
3:特性测试类
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 特性_反射
{
[Table("Consumers")]
public class UserInf
{
private string _UserID;
/// <summary>
/// 登陆ID
/// </summary>
[Field("ConsumerID", DbType.String, )]
public string U_UserID
{
get { return _UserID; }
set { _UserID = value; }
} private string _Psw;
/// <summary>
/// 登陆密码
/// </summary>
[Field("ConsumerPwd", DbType.String, )]
public string U_Psw
{
get { return _Psw; }
set { _Psw = value; }
} private string _UserName;
/// <summary>
/// 用户别称
/// </summary>
[Field("ConsumerName", DbType.String, )]
public string U_UserName
{
get { return _UserName; }
set { _UserName = value; }
} private string _City;
/// <summary>
/// 所住城市
/// </summary>
[Field("UserCity", DbType.String, )]
public string U_City
{
get { return _City; }
set { _City = value; }
} private int _Popedom;
/// <summary>
/// 权限
/// </summary>
[Field("popedom", DbType.Int32, )]
public int U_Popedom
{
get { return _Popedom; }
set { _Popedom = value; }
} private DateTime _AddDataTime;
/// <summary>
/// 注册时间
/// </summary>
[Field("addDataTime", DbType.Date, )]
public DateTime U_AddDataTime
{
get { return _AddDataTime; }
set { _AddDataTime = value; }
} private int _Sex;
/// <summary>
/// 性别
/// </summary>
[Field("Sex", DbType.Int32, )]
public int U_Sex
{
get { return _Sex; }
set { _Sex = value; }
} private int _BirthTime;
/// <summary>
/// 出身日期;
/// </summary>
[Field("BirthTime", DbType.String, )]
public int U_BirthTime
{
get { return _BirthTime; }
set { _BirthTime = value; }
}
}
}
4:测试控制台程序
UserInf userss = new UserInf();
userss.U_UserID = "aw12311";
userss.U_Psw = "";
userss.U_UserName = "aw";
userss.U_City = "武汉";
userss.U_Popedom = ;
userss.U_Sex = ;
userss.U_BirthTime = ;
userss.U_AddDataTime = DateTime.Now; DateIsTableAttribute<UserInf> t = new DateIsTableAttribute<UserInf>(); Response.Write(" </br>" + t.insertDate(userss));
http://www.cnblogs.com/sosoft/
最新文章
- const extern static 终极指南
- Voix.js – 使用声音来控制和操纵你的网站
- 编译Android系统源码(高通平台)
- mysql的隐式转化
- ios实现程序切入后台,实现后台任务 (转自)
- android Java BASE64编码和解码一:基础
- [LintCode] Trapping Rain Water
- 【MySQL】MySQL的group_concat使用例子
- linux查看cpu、内存信息
- C#程序员整理的Unity 3D笔记(十五):Unity 3D UI控件至尊–NGUI
- python学习笔记3(字符串)
- CVPR2013-reading list
- CTE-递归[2]
- 解决方案--java执行cmd命令ProcessBuilder--出错Exception in thread "main" java.io.IOException: Cannot run program "dir d:\": CreateProcess error=2(xjl456852原创)
- Vue + vue-router
- C# 中判断字符串是不是汉字
- SOCKET 编程TCP/IP、UDP
- File operations 1
- 2019 Power BI最Top50面试题,助你面试脱颖而出系列<;中>;
- vs开发/IIS 遇到的问题--------笔记
热门文章
- ASP.NET Core Kestrel 中使用 HTTPS (SSL)
- c# 元组Tuple
- Centos 使用YUM安装MariaDB
- 在Linux上编译dotnet cli的源代码生成.NET Core SDK的安装包
- MySQL 远程连接(federated存储引擎)
- 我的LESS编译方案
- [ASP.NET MVC 小牛之路]10 - Controller 和 Action (2)
- 虚拟机网络驱动(共享文件夹)不见了的解决方案-适用于win7~win10 and Windows Server 2008~Windows Server 2012R2
- [汇编与C语言关系]2. main函数与启动例程
- 感恩回馈,《ASP.NET Web API 2框架揭秘》免费赠送