通过自定义注解反射生成SQL语句
----------------------------------------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;
}
}
}
通过自定义注解反射生成SQL语句的更多相关文章
- C# - 通过自定义注解反射生成SQL语句[转]
转自http://blog.163.com/jong_cai/blog/static/87028045200902033553581/ -------------------------------- ...
- 4、注解反射生成SQL语句
.任务说明 ①有一张用户表,字段包括:用户ID.用户名.昵称.年龄.性别.所在城市.邮箱.手机号: ②使用java注解来对用户表的每个字段或字段的组合条件进行动态生成S ...
- ASP.NET通过反射生成sql语句
最近对接一个接口,需要通过xml序列化成实体后添加额外信息后批量插入数据库,需要手动拼sql.因为涉及多张表,拼凑很麻烦而且容易出错,所以写了两个工具方法来生成sql,先写到博客里面,以便以后不时之需 ...
- 反射生成SQL语句入门
今天我们来学习学习通过反射技术来生成SQL语句. 反射提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型.然后,可以调用类型的方法或访 ...
- 利用反射生成SQL语句
// 修改学员信息的方法 public bool ModifyStudent(MODEL.Students model) { // 利用反映获取类对所有属性,用来动态生成SQL语句 StringBui ...
- 根据反射生成SQL语句
/** * 基础查询语句 * 返回类型的属性字符串Sql * @author: InkYi * 修改时间:2016年5月11日 - 上午10:06:00<br/> * 功能说明:<b ...
- 反射生成SQL语句
public static int Reg(Model ml) { bool b = true; Visit vt = new Visit(); StringBuilder builder = new ...
- Java 自定义注解及注解读取解析--模拟框架生成SQL语句
假设们使用一张简单的表,结构如下: 定义注解: 表注解: package com.xzlf.annotation; import java.lang.annotation.ElementType; i ...
- 利用反射自动生成SQL语句(仿Linq)
转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465597.html using System; using System.Colle ...
随机推荐
- C#整理4——循环语句
一.循环语句 定义:可以反复执行某段代码,直到不满足循环条件为止. 循环的四要素:初始条件.循环条件.状态改变.循环体. 1.初始条件:循环最开始的状态. 2.循环条件:在什么条件下进行循环,不满足 ...
- Fiddler 跟踪 手机页面数据包
随着 HTML5 的急速增长,现在越来越多的人,开始涉及到移动终端的 Web 开发领域,但手机端始终没有 PC 端这么多的调试工具.即使 PC 端浏览器模拟 user-agent 进行开发,也可能会发 ...
- UVA 10954 Add All
题意: 给出n个数,要将n个数相加,每次相加所得的值为当次的计算量,完成所有的求和运算后,要求总的计算量最小. 分析: 直接一个优先队列,由小到大排序,每次前两个相加就好. 代码: #include ...
- 蜗牛爱课 -- iOS 设计模式之模板模式
1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...
- IntPtr问题
public aaa(IntPtr myPtr,int left, int top, int width, short height) 这里myPtr应该是对应到一块内存,你需要查看aaa函数是如何把 ...
- centOs下的php+mysql+apache+ftp配置
在安装服务器时做了相应的笔记,这个方法是亲身经验成功的,随着版本的不断更新,也许会有一些地方不同,但是基本原理都是一样的. 1.安装CentOS 6 ,可以选择最小安装,也可以安装桌面 2.升级系统 ...
- 点击上下页,实现图片滚动的jquery代码
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- Android 测试工具集01
Appium是一个支持原生,混合和移动web apps的开源的跨平台测试框架工具. ANDROID依赖 Android SDK API >= 17 (Additional features re ...
- Python之路第五天,基础(5)-序列化和字符串格式化
序列化 Python中用于序列化的两个模块 json 用于『字符串』和『python基本数据类型』间进行转换 pickle 用于『python特有的类型』和『python基本数据类型』间进行转换 js ...
- 查看Linux下网卡状态或 是否连接
分类: 1) 通过mii-tool指令 [root@localhost root]# mii-tool eth0: negotiated 100baseTx-FD, link ...