通过自定义注解反射生成SQL语句
2016-03-18 11:41
567 查看
----------------------------------------Program.cs----------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
//反射命名空间
using System.Reflection;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student(1, "Jong_Cai", 21);
Insert(stu);
}
public static void Insert(Object obj)
{
String fileds = null;
String values = null;
Type type = obj.GetType();
//获取类名
String className = type.Name;
//获取所有公有属性
PropertyInfo[] info = type.GetProperties();
foreach (PropertyInfo var in info)
{
//取得属性的特性标签,false表示不获取因为继承而得到的标签
Object[] attr = var.GetCustomAttributes(false);
if (attr.Length > 0)
{
//从注解数组中取第一个注解(一个属性可以包含多个注解)
MyAttribute myattr = attr[0] as MyAttribute;
if (myattr.PrimaryKey == true)
{
continue;
}
}
fileds += var.Name + ",";
values += "'" + var.GetValue(obj, null) + "',";
}
fileds = fileds.Substring(0, fileds.Length - 1);
values = values.Substring(0, values.Length - 1);
String sql = "insert into {0}({1}) values({2})";
sql = String.Format(sql, className, fileds, values);
Console.WriteLine(sql);
}
}
}
-----------------------------------------------MyAttribute.cs---------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
//自定义注解类
class MyAttribute: Attribute
{
public Boolean PrimaryKey = false;
public String Type = null;
}
}
-------------------------------------------Student.cs--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class Student
{
private int _id;
[My(PrimaryKey = true, Type = "自动增长")] //自定义注解
public int Id
{
get { return _id; }
set { _id = value; }
}
private String _name;
[My(PrimaryKey = false, Type = "名字")] //自定义注解
public String Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
[My(PrimaryKey = false, Type = "年龄")] //自定义注解
public int Age
{
get { return _age; }
set { _age = value; }
}
public Student(int id, String name, int age)
{
this._id = id;
this._name = name;
this._age = age;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
//反射命名空间
using System.Reflection;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Student stu = new Student(1, "Jong_Cai", 21);
Insert(stu);
}
public static void Insert(Object obj)
{
String fileds = null;
String values = null;
Type type = obj.GetType();
//获取类名
String className = type.Name;
//获取所有公有属性
PropertyInfo[] info = type.GetProperties();
foreach (PropertyInfo var in info)
{
//取得属性的特性标签,false表示不获取因为继承而得到的标签
Object[] attr = var.GetCustomAttributes(false);
if (attr.Length > 0)
{
//从注解数组中取第一个注解(一个属性可以包含多个注解)
MyAttribute myattr = attr[0] as MyAttribute;
if (myattr.PrimaryKey == true)
{
continue;
}
}
fileds += var.Name + ",";
values += "'" + var.GetValue(obj, null) + "',";
}
fileds = fileds.Substring(0, fileds.Length - 1);
values = values.Substring(0, values.Length - 1);
String sql = "insert into {0}({1}) values({2})";
sql = String.Format(sql, className, fileds, values);
Console.WriteLine(sql);
}
}
}
-----------------------------------------------MyAttribute.cs---------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
//自定义注解类
class MyAttribute: Attribute
{
public Boolean PrimaryKey = false;
public String Type = null;
}
}
-------------------------------------------Student.cs--------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
public class Student
{
private int _id;
[My(PrimaryKey = true, Type = "自动增长")] //自定义注解
public int Id
{
get { return _id; }
set { _id = value; }
}
private String _name;
[My(PrimaryKey = false, Type = "名字")] //自定义注解
public String Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
[My(PrimaryKey = false, Type = "年龄")] //自定义注解
public int Age
{
get { return _age; }
set { _age = value; }
}
public Student(int id, String name, int age)
{
this._id = id;
this._name = name;
this._age = age;
}
}
}
相关文章推荐
- linux 自启动oracle脚本(使用oracle自带脚本)
- centos7 配置开机自启redis
- MYSQL5.7.9开启慢查询日志的技巧
- 根据从redis缓存的数据查询出来,在从数据库中取出所有的数据,俩个数据进行比较,去掉重复,剩下库中新插入的数据,取出新数据,然后把redis中的缓存数据清空把从数据库中查出来的所有数据放到redis缓存中
- Memcache Brief Intro
- oracle解析xml(增加对9i版本的支持)
- wamp默认配置修改
- MySQL 中文显示乱码以及中文查询条件返回0条结果的问题解决
- Hibernate使用原生SQL多表查询时字段名相同导致查询数据覆盖问题解决办法
- MySQL常用配置
- 开篇:准备开始写SQL的成长历程啦~
- Oracle Linux 6.5 安装Oracle 11G 配置
- CUBRID学习笔记 41 sql语法之select
- 强大的数据库操作类DBHelper
- 基础知识收录(三)MYSQL大小写敏感
- 利用sqoop将hive和mysql数据互导简单实验
- 图文:通过sql server 连接mysql
- MySQL灾难恢复实战
- oracle 11g dataguard 出现ORA-16143: 终端恢复过程中或之后不允许进行 RFS 连接解决方法
- Sql 存储过程动态添加where条件