转自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语句[转]的更多相关文章

  1. 通过自定义注解反射生成SQL语句

    ----------------------------------------Program.cs---------------------------------------- using Sys ...

  2. 4、注解反射生成SQL语句

    .任务说明         ①有一张用户表,字段包括:用户ID.用户名.昵称.年龄.性别.所在城市.邮箱.手机号:         ②使用java注解来对用户表的每个字段或字段的组合条件进行动态生成S ...

  3. ASP.NET通过反射生成sql语句

    最近对接一个接口,需要通过xml序列化成实体后添加额外信息后批量插入数据库,需要手动拼sql.因为涉及多张表,拼凑很麻烦而且容易出错,所以写了两个工具方法来生成sql,先写到博客里面,以便以后不时之需 ...

  4. 反射生成SQL语句入门

    今天我们来学习学习通过反射技术来生成SQL语句. 反射提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型.然后,可以调用类型的方法或访 ...

  5. 利用反射生成SQL语句

    // 修改学员信息的方法 public bool ModifyStudent(MODEL.Students model) { // 利用反映获取类对所有属性,用来动态生成SQL语句 StringBui ...

  6. 根据反射生成SQL语句

    /** * 基础查询语句 * 返回类型的属性字符串Sql * @author: InkYi * 修改时间:2016年5月11日 - 上午10:06:00<br/> * 功能说明:<b ...

  7. 反射生成SQL语句

    public static int Reg(Model ml) { bool b = true; Visit vt = new Visit(); StringBuilder builder = new ...

  8. Java 自定义注解及注解读取解析--模拟框架生成SQL语句

    假设们使用一张简单的表,结构如下: 定义注解: 表注解: package com.xzlf.annotation; import java.lang.annotation.ElementType; i ...

  9. 利用反射自动生成SQL语句(仿Linq)

    转:http://www.cnblogs.com/the7stroke/archive/2012/04/22/2465597.html using System; using System.Colle ...

随机推荐

  1. iPhone/iTouch免99美刀真机调试

    本文经本人验证,攻略来源于网上,由于多次转载原始出处不可靠,故无法对原作者进行链接引用,抱歉. 本文仅为记录流程,以备日后查询.本文版权所无,欢迎转载和拍砖. 测试环境: XCode 4.0.2 + ...

  2. Linux(ubuntu)使用dd从iso制作win7安装u盘(读卡器一样),以及备份分区

    转自我的独立博客:alanslab.cn 先吐个槽,早知道windows越用越慢,没想到太久不用还会坏... 昨天就遇到了这样一件事,电脑的光驱早拆了换硬盘托架了,只好翻出来以前手机的sd卡开刀,网上 ...

  3. label 标签

    <label> 标签为 input 元素定义标注内容 label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发此控件.就是 ...

  4. 执行startx后Ubuntupassword正确进不去的问题

    今天在命令行里敲了 startx ,然后系统重新启动.输入password后,跳转到一下界面.之后又返回到登陆界面.一直这样循环输入password.进不去系统. 然后不得不用手机在网上查找解决的方法 ...

  5. Ubuntu下屏幕录像、后期处理不完全攻略

    提要 如果要做成果展示或者效果演示,通常需要录取屏幕生成视频文件,在windows中我们可以用屏幕录像专家在录像, vegas 来做后期处理,Ubuntu可以么? 答案时当然可以!虽然第一次用觉得有点 ...

  6. 前端新人学习笔记-------html/css/js基础知识点

    即将毕业的软件工程大学生一枚,秋季招聘应聘的是Android,今年来到公司实习,要求做前端开发,所以一切只有现学,现在根据视频来学习,然后开这个博客记录一下自己的学习过程,废话不多说,开写. 4月6日 ...

  7. 微信开发-Jssdk调用分享实例

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...

  8. xmanager 使用

    linux 上安装xterm windows上启动命令: /usr/bin/xterm -ls -display $DISPLAY

  9. python定制类(以Fib类为例)

    class Fib(object): def __init__(self): self.a, self.b = 0, 1 def __iter__(self): return self def __n ...

  10. hdu 4454 Stealing a Cake (三分)

    Stealing a Cake Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...