实体类基类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Common
{
/// <summary>
/// 实体类基类
/// </summary>
[Serializable]
public abstract class EntityBase
{
/// <summary>
/// 获取主键
/// </summary>
/// <returns></returns>
public abstract string GetPrimaryKey();
/// <summary>
/// 获取INSERT语句
/// </summary>
/// <returns></returns>
public string GetInsertSql()
{
try
{
Type t = this.GetType();
string tableName = t.Name,pKey=this.GetPrimaryKey(),fields=string.Empty,values=string.Empty,temp=null;
foreach (PropertyInfo pi in t.GetProperties())
{
if (!pi.CanWrite) continue;
if (pi.Name.Equals(pKey))
{
continue;
}
temp = GetByTypeStr(pi);
fields += pi.Name + ",";
values += temp + ",";
}
return string.Format("Insert into {0}({1}) Values({2})", tableName, fields.TrimEnd(','), values.TrimEnd(','));
}
catch
{
throw;
}
}
/// <summary>
/// 获取UPDATE语句
/// </summary>
/// <returns></returns>
public string GetUpdateSql()
{
try
{
Type t = this.GetType();
PropertyInfo[] pInfos = t.GetProperties();
string tableName = t.Name, pKey = this.GetPrimaryKey(), str_fields=string.Empty;
int keyIndex = -;
for (int i = ; i < pInfos.Length; i++)
{
if (pInfos[i].Name.Equals(this.GetPrimaryKey()))
{
keyIndex = i;
continue;
}
str_fields += pInfos[i].Name + " = " + GetByTypeStr(pInfos[i]) + ",";
}
return string.Format("Update {0} Set {1} Where {2} = {3}", tableName, str_fields.TrimEnd(','),this.GetPrimaryKey(), GetByTypeStr(pInfos[keyIndex]));
}
catch
{
throw;
}
}
/// <summary>
/// 根据数据类型反射字段值
/// </summary>
/// <param name="pInfo">公共属性</param>
/// <returns></returns>
private string GetByTypeStr(PropertyInfo pInfo)
{
try
{
string result_str = string.Empty;
Type t = pInfo.PropertyType;
object obj = pInfo.GetValue(this, null);
bool valueNull = StringUtil.isNullOrBlank(obj);
if (t == typeof(string))
{
result_str = valueNull ? "null" : "'" + obj.ToString().Replace("--","") + "'";
}
else if (t == typeof(System.Decimal) || t == typeof(System.Int16) || t == typeof(System.Int32) || t == typeof(System.Int64))
{
result_str = t.Name == "Nullable`1"&& valueNull ? "null" : obj.ToString();
//if ()
//{ //}
//else
//{
// result_str = valueNull ? "0" : obj.ToString();
//}
}
else if(t==typeof(DateTime)||t.Name== "Nullable`1")
{
if (valueNull||DateTime.MinValue.Equals(obj)|| t.Name == "Nullable`1")
{
result_str = "null";
}
else
{
result_str = "'"+obj.ToString().Replace("年", "-").Replace("月", "-").Replace("日", "")+"'";
}
}
return result_str;
}
catch
{
throw;
}
}
}
}

实体类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common; namespace Model
{
public class MainModel:EntityBase
{
public decimal id { get; set; }
public string title { get; set; }
public string contents { get; set; }
public string type { get; set; }
public DateTime? date { get; set; }
public string people { get; set; }
public string picurl { get; set; }
/// <summary>
/// 设置主键
/// </summary>
/// <returns></returns>
public override string GetPrimaryKey()
{
return "id";
}
}
}

调用:

             Model.MainModel model = new Model.MainModel();
model.title = context.Request.Form["txtTitle"];
model.people = context.Request.Form["txtName"];
model.contents = context.Request.Form["txtContent"];
string resSql = model.GetInsertSql();

反射实体类拼接SQL语句的更多相关文章

  1. Java代码实体类生成SQL语句(Java实体类转数据库)

    有的时候把数据库删了,如果照着实体类重新创建数据库的话比较麻烦,可以使用这个工具,把代码复制到项目里面设置一下即可把Java代码中的实体类转换为SQL语句输出为一个文件,打开执行命令即可. 下载:ht ...

  2. c#自定义ORM框架---(泛型&反射&实体类扩展属性<附带通用增、删、查、改>)

    该教材主要是运用到泛型.反射和实体类扩展属性 步骤一.建立扩展属性类 实体类扩展属性要继承Attribute基类完成 [AttributeUsage(AttributeTargets.Property ...

  3. EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的

    我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...

  4. StringBuilder 拼接sql语句比较快

    StringBuilder 拼接sql语句比较快StringBuilder strBuilder = new StringBuilder();strSql += "insert into t ...

  5. ASP.NET实现列表页连接查询 拼接sql语句 绑定grivdView

    ASP.NET实现列表页连接查询 拼接sql语句 如图效果: 基本需求:1.当页面第一次加载的时候默认查询一个月时间(或者说是登陆者所属权限的所有数据)的数据绑定到gridView 2.添加查询条件时 ...

  6. 查询拼接SQL语句,多条件模糊查询

    多条件查询,使用StringBuilder拼接SQL语句,效果如下: 当点击按钮时代码如下: private void button1_Click(object sender, EventArgs e ...

  7. java动态拼接sql语句并且执行时给sql语句的参数赋值

    问题 在这里举一个例子,比如我要做一个多条件模糊查询,用户输入的时候有可能输入一个条件,也有可能输入两个条件,这时执行查询的sql语句就不确定了,但可以用动态拼接sql语句来解决这个问题. 解决方法 ...

  8. java反射获取注解并拼接sql语句

    先建两个注解 分别为 Table 和 Column package com.hk.test; import java.lang.annotation.ElementType; import java. ...

  9. 模拟实现MyBatis中通过SQL反射实体类对象功能

    话不多说,直接上干货! package cn.test; import java.lang.reflect.Method; import java.sql.Connection; import jav ...

随机推荐

  1. 基于Spring Boot+Spring Security+JWT+Vue前后端分离的开源项目

    一.前言 最近整合Spring Boot+Spring Security+JWT+Vue 完成了一套前后端分离的基础项目,这里把它开源出来分享给有需要的小伙伴们 功能很简单,单点登录,前后端动态权限配 ...

  2. C# 同步转异步 TaskCompletionSource

    当我们遇到一些异步执行又无法等待时的逻辑,比如动画的执行. 而业务上又需要等待逻辑的完成,再去处理后续的操作.这时需要转成异步方法 如下,同步执行一个动画后,再输出日志: private async ...

  3. 关于C# webapi ,接口返回字符串和json格式 ,返回值中有反斜杠

    最近遇到一个比较郁闷的问题,记录一下 写了一个接口,想返回json 数据,但是返回值中总是带有反斜杠... ,下面来看原因 首先,配置 webapi的路由 App_Start 文件夹下 ,WebApi ...

  4. Hadoop、storm和Spark Streaming简单介绍(非原创)

    文章大纲 一.Hadoop是什么二.storm是什么三.Spark Streaming是什么四.Spark与storm比较五.参考文章   一.Hadoop是什么 1. 简介 Hadoop是一个由Ap ...

  5. bay——安装_Oracle 12C-RAC-Centos7.txt

    ★★★____★☆★〓〓〓〓→2019年6月26日10:29:42 bayaim-RAC ——搭建第4次VMware vSphere Client6.0 ----------------------- ...

  6. Git 在小团队中的管理流程

    目标读者:了解 Git 的基本概念,能够使用 Git 进行基本的本地和远程操作. 有关 Git 的基础知识可以参见 知乎回答-怎样使用 GitHub?,天猪(刘勇)给出了一些很好的学习资料. 本文介绍 ...

  7. for(var i in items) 和 for(var i;i<items.length;i++) 区别

    前者循环的是属性,后者循环的才是数组. 若项目中对数组属性进行了扩展,那切记不能使用前者,否则在循环数组时扩展的函数体也会被当做数据返回. var data = { p1:1, p2:"b& ...

  8. AutoCAD配置的Heidi驱动程序未加载

    电脑安装的软件越来越多,有的软件也就偶尔使用一下下,于是就找了一个绿化版的AutoCAD,挺好的,可启动时弹出"配置的Heidi驱动程序未加载.切换到默认软件驱动程序". 对于上述 ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造

    C. Messy You are fed up with your messy room, so you decided to clean it up. Your room is a bracket ...

  10. 基于UDP协议的socket套接字编程

    目录 一.UDP套接字简单示例 1.1 服务端 二.客户端 三.UPD套接字无粘包问题 3.1 服务端 3.2 客户端 四.qq聊天 4.1 服务端 4.2 客户端1 4.3 客户端2 4.4 运行结 ...