C# - 通过自定义注解反射生成SQL语句[转]
转自http://blog.163.com/jong_cai/blog/static/87028045200902033553581/
----------------------------------------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;
}
}
}
C# - 通过自定义注解反射生成SQL语句[转]的更多相关文章
- 通过自定义注解反射生成SQL语句
----------------------------------------Program.cs---------------------------------------- using Sys ...
- 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 ...
随机推荐
- Oracle EBS-SQL (SYS-21):sys_用户名与人员对应关系查询.sql
select fu.user_name 用户名, fu.description 描述, (select ppf.FULL_NAME ...
- ios jsbrige
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- BZOJ 1143 [CTSC2008]祭祀river(二分图匹配)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1143 [题目大意] 给出一张有向图,问最大不连通点集,连通具有传递性 [题解] 我们将 ...
- Day4_代码重用与函数
知识点速记: 重用代码的方法:脚本包含require().include(); 全局配置文件php.ini(auto_prepend_file/auto_append_file); 目录配置文件.ht ...
- redsocks 设置全局代理
分享一下我的经验, http://lilydjwg.is-programmer.com/2014/2/7/linux-really-global-http-proxy.42701.html 1. 优点 ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(二)
这篇文章的理解,需要一些专业知识了. 我们可以创建模拟自己的外设吗? 我们已经知道什么是qemu了,我们可以通过qmeu的提供的外设,DIY一个计算机了. 但是我们可能还不满足,我们可以自己制造一个外 ...
- Android UI高级交互设计Demo
首先:是google的新标准 Google Material design 开源项目 1.直接拿来用!十大Material Design开源项目 2.收集android上开源的酷炫的交互动画和视觉效果 ...
- CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\921bbfc4\ca7cf42\App_Code.fu98jwep.dll”--“拒绝访问。 ”
在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Fil ...
- hadoop之hdfs学习
简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...
- R与数据分析旧笔记(六)多元线性分析 上
> x=iris[which(iris$Species=="setosa"),1:4] > plot(x) 首先是简单的肉眼观察数据之间相关性 多元回归相较于一元回归的 ...