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. windows phone SDK 8.0 模拟器异常 0x89721800解决办法

    删除 APPDATA\LOCAL\Microsoft\Phone Tools\CoreCon\10.0 从新启动即可!

  2. 人性的弱点&&影响力

    How wo win friends and influence people 人性的弱点 by 卡耐基 人际关系基本技巧 不要批评.谴责.抱怨 真诚的欣赏他人 激发他人的渴望 获得别人好感的方式 微 ...

  3. C# 指针操作图像 细化处理

    /// <summary> /// 图形细化 /// </summary> /// <param name="srcImg"></para ...

  4. Cygwin的安装,卸载,以及安装gdb

    转载来源 http://10000001.blog.51cto.com/4600383/1341484   1.安装 其实Cygwin的安装时很简单的,需要的安装相应的就可以了,要详细的去网上找,很多 ...

  5. Understanding Kafka Consumer Groups and Consumer Lag

    In this post, we will dive into the consumer side of this application ecosystem, which means looking ...

  6. 编译报错dereferencing pointer to incomplete type

    关于编译报错“dereferencing pointer to incomplete type... 多是没找到结构体的定义,可以在本地复制其定义试试. 参考: http://my.oschina.n ...

  7. 30分钟LINQ教程(转)

    在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...

  8. SpringJMS解析3-监听器

    消息监听器容器是一个用于查看JMS目标等待消息到达的特殊bean,一旦消息到达它就可以获取到消息,并通过调用onMessage()方法将消息传递给一个MessageListener实现.Spring中 ...

  9. Uva12206 Stammering Aliens 后缀数组&&Hash

    Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all effort ...

  10. spring-boot项目在外部tomcat环境下部署

    http://m.blog.csdn.net/article/details?id=51009423