static void Main(string[] args)
{
book book = new book();//实体类
booktest b1 = new booktest(); book.bookid = "";
book.bookname = "计算机原理";
book.bookprice = 32.04M;
string sql = CreateInsertSQL(book);
} public static string CreateInsertSQL(book book)
{
Type type = book.GetType(); PropertyInfo[] props = type.GetProperties();
StringBuilder sb = new StringBuilder();
sb.Append("insert into "+ type.Name+"(");
foreach( PropertyInfo prop in props)
{
string name = prop.PropertyType.FullName;
string value = prop.GetValue(book,null) as string;
object[] array = prop.GetCustomAttributes(typeof(KEYAttribute),true);//获取属性,判断在sql语句中必须的,比如主键
if (array.Length > )
{
continue;
}
sb.Append(prop.Name + ","); }
sb.Remove(sb.Length - , );
sb.Append(") values(");
foreach (PropertyInfo prop in props)
{
object[] array = prop.GetCustomAttributes(typeof(KEYAttribute), true);
if (array.Length > )
{
continue;
}
sb.Append("@" + prop.Name + ",");
} sb.Remove(sb.Length-,);
sb.Append(")");
return sb.ToString();
}

C#反射生成简单sql语句的更多相关文章

  1. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句

    这次给大家介绍一下在Java开发过程中 使用自定义注解开发:主要知识点:            1.反射            主要用于提取注解信息            2.自定义异常  主要是为了 ...

  2. 简单理解ORM,实体类生成查询SQL语句

    目前有很多开源的ORM项目,大多情况下也不需要我们重复去造轮子,我们只需要了解轮子怎么造的,怎么用就可以,下面简单说一下怎么通过实体生成一个SQL语句: 先建立2个Attribute类,TableAt ...

  3. 使用EntityFramework6.1的DbCommandInterceptor拦截生成的SQL语句

    开始 EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.Entity.Infrastructure.Interception 命名空间,此命名空间下的对象可以 ...

  4. 查看Linq to Sql生成的sql语句(转)

    查看Linq to Sql生成的sql语句   在控制台项目中,比较简单,直接db.Log = Console.Out;就OK了 但是在其他项目中,需要处理如下: StreamWriter sw = ...

  5. [转]使用EntityFramework6.1的DbCommandInterceptor拦截生成的SQL语句

    本文转自:http://www.cnblogs.com/Ax0ne/p/3620958.html 开始 EF6.1也出来不少日子了,6.1相比6.0有个很大的特点就是新增了System.Data.En ...

  6. 监听Entity Framework生成的Sql语句

               Entity Framework为我们提供了很大的方便,但有时候,我们想看看EF生成的Sql语句到底是怎样的,一种方式是我们可以启用Sql Server Profer工具.今天介 ...

  7. Entity Framewrok 7beta7中不同版本sql server自动生成分页sql语句的问题

    在EF中,使用linq进行分页是很方便的,假如我们有一个EMP表,结构如下: public class Emp { [Key] public Guid No { get; set; } public ...

  8. .NET Core实用技巧(一)如何将EF Core生成的SQL语句显示在控制台中

    目录 .NET Core实用技巧(一)如何将EF Core生成的SQL语句显示在控制台中 前言 笔者最近在开发和维护一个.NET Core项目,其中使用几个非常有意思的.NET Core相关的扩展,在 ...

  9. 用LinqPad查看Nhibernate生成的sql语句

    使用Nhibernate开发一般都要对Nhibernate生成的sql语句进行查看及分析,查看Nhibernate生成的sql语句,可以使用NHProfiler和log4net.但NHProfiler ...

随机推荐

  1. Active Record 数据库模式-增删改查操作

    选择数据 下面的函数帮助你构建 SQL SELECT语句. 备注:如果你正在使用 PHP5,你可以在复杂情况下使用链式语法.本页面底部有具体描述. $this->db->get(); 运行 ...

  2. linux c学习笔记----互斥锁属性

    转自:http://lobert.iteye.com/blog/1762844 互斥锁属性 使用互斥锁(互斥)可以使线程按顺序执行.通常,互斥锁通过确保一次只有一个线程执行代码的临界段来同步多个线程. ...

  3. Objective-C编码规范:26个方面解决iOS开发问题(转)

    链接

  4. 用Feature的方式删除SharePoint2010的Page中重复的WebPart

    用Feature的方式删除SharePoint2010的Page中重复的WebPart. 代码如下所示: public class SupportCenterDuplicatedWebpartRemo ...

  5. POJ 1987 Distance Statistics 树分治

    Distance Statistics     Description Frustrated at the number of distance queries required to find a ...

  6. 如果 if - 迈克.杰克逊的墓志铭

    引用http://www.duwenzhang.com/wenzhang/yingyuwenzhang/20110215/171059.html IF you can keep your head w ...

  7. RPC和Socket,RMI和RPC之间的关系

    远程通信机制RPC与RMI的关系 http://blog.csdn.net/zolalad/article/details/25161133       1.RPC RPC(Remote Proced ...

  8. ThinkPHP去重 distinct和group by

    转自:http://blog.csdn.net/helencoder/article/details/50328629 近期项目中,遇到数据表去重要求,对于ThinkPHP的去重有了更加准确的认识和体 ...

  9. 本人经过测试认为最简单最好的popupwindow样式

    <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- solid 设置 ...

  10. css整理-03 文本

    缩进和水平对齐 缩进文本:text-indent 可以设置为负值 可以为所有块级元素应用,但无法应用到行内元素,图像之类的替换元素: 水平对齐: text-align left,center,righ ...