添加下面这个类

public static class GetAllAttribute<T> where T : class
{
public static string Names;
public static string Values;
public static ArrayList array;
/// <summary>
/// 获取当前对象的所有属性名称,以逗号隔开
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static string GetNames(T entity)
{
PropertyInfo[] p = entity.GetType().GetProperties();
string name = string.Empty;
foreach (PropertyInfo item in p)
{
name += item.Name + ",";
}
name = name.Substring(0, name.Length - 1);
return name;
}
/// <summary>
/// 获取当前对象的所有属性值,以逗号隔开
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static string GetValues(T entity)
{
PropertyInfo[] p = entity.GetType().GetProperties();
string value = string.Empty;
foreach (PropertyInfo item in p)
{
if (item.PropertyType.Name.Equals("String"))
{
value += "'" + item.GetValue(entity, null) + "',";
}
if (item.PropertyType.Name.Equals("Int32"))
{
value += item.GetValue(entity, null) + ",";
}
if (item.PropertyType.Name.Equals("DateTime"))
{
value += "to_Date('" + item.GetValue(entity, null) + "','yyyy-MM-dd HH:mm:ss'),";
}
}
value = value.Substring(0, value.Length - 1);
return value;
}
/// <summary>
/// 根据条件设置字段名称和字段对应的值
/// </summary>
/// <param name="entity"></param>
/// <param name="names"></param>
public static void SetNamesAndValues(T entity, string names)
{
Names = string.Empty;
Values = string.Empty;

string[] _Names = names.Split(',');
PropertyInfo[] p = entity.GetType().GetProperties();
foreach (PropertyInfo item in p)
{
bool flg1 = false;
for (int i = 0; i < _Names.Length; i++)
{
string str_Name = _Names[i];
bool flg = item.Name.Equals(str_Name);
if (flg)
{
flg1 = true;
}
}
if (!flg1)
{
Names += item.Name + ",";
Values += item.GetValue(entity, null) + ",";
}
}
Names = Names.Substring(0, Names.Length - 1);
Values = Values.Substring(0, Values.Length - 1);
}
/// <summary>
/// 根据条件设置字段名称和字段对应的值
/// </summary>
/// <param name="entity"></param>
/// <param name="names"></param>
public static void SetNamesAndValues1(T entity, string names)
{
Names = string.Empty;
Values = string.Empty;

string[] _Names = names.Split(',');
PropertyInfo[] p = entity.GetType().GetProperties();
foreach (PropertyInfo item in p)
{
bool flg1 = false;
for (int i = 0; i < _Names.Length; i++)
{
string str_Name = _Names[i];
bool flg = item.Name.Equals(str_Name);
if (flg)
{
flg1 = true;
}
}
if (flg1)
{
Names += item.Name + ",";
Values += item.GetValue(entity, null) + ",";
}
}
Names = Names.Substring(0, Names.Length - 1);
Values = Values.Substring(0, Values.Length - 1);
}

#region 获取Insert语句
/// <summary>
/// 获取Insert语句
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static string Insert(T entity)
{
try
{
array = new ArrayList();
Type type = entity.GetType();
PropertyInfo[] p = type.GetProperties();
//表名
string tableName = "";
foreach (Object attr in type.GetCustomAttributes(false))// 遍历该属性的所有注解
{
// 判断是否有属性修饰,并判断属性值
if (attr is TableAttribute)
{
TableAttribute currAttribute = attr as TableAttribute;
if (!string.IsNullOrEmpty(currAttribute.TableName))
{
tableName = currAttribute.TableName;
}
}
}
//string strSql = "Insert into " + tableName + "(";
string strSql = "Insert into {0}({1}) values({2})";

//字段名称列表,以逗号隔开
string Files = "";
//字段名称对应的值的列表,以逗号隔开
string Values = "";

foreach (PropertyInfo item in p)
{
OracleParameter par = null;

//当前字段值
string value = item.GetValue(entity, null) + "";
string name = item.Name;
//当前值的类型(String,Int32,Decimal,Double,DateTime)
string Pname = item.PropertyType.Name;

foreach (Object attr in item.GetCustomAttributes(false))// 遍历该属性的所有注解
{
// 判断当前字段是否是数据库字段
if (attr is FieldAttribute)
{
FieldAttribute fileld = attr as FieldAttribute;

if (!string.IsNullOrEmpty(fileld.Field))
{
name = fileld.Field;
}
}
// 判断当前字段是否是主键
else if (attr is PrimaryKeyAttribute)
{
PrimaryKeyAttribute currAttribute = attr as PrimaryKeyAttribute;
if (!string.IsNullOrEmpty(currAttribute.Id))
{
name = currAttribute.Id;
}
}
Files += name + ",";
Values += ":" + name + ",";

if (Pname.Equals("String"))
{
par = new OracleParameter(name, OracleDbType.Varchar2);
par.Value = value;
array.Add(par);
}
else if (Pname.Equals("Int32"))
{
par = new OracleParameter(name, OracleDbType.Int32);
int temp = Convert.ToInt32(value);
if (temp <= -1)
{
par.Value = DBNull.Value;
}
else
{
par.Value = temp;
}
array.Add(par);
}
else if (Pname.Equals("Decimal"))
{
par = new OracleParameter(name, OracleDbType.Decimal);
decimal temp = Convert.ToDecimal(value);
if (temp <= -1)
{
par.Value = DBNull.Value;
}
else
{
par.Value = temp;
}
array.Add(par);
}
else if (Pname.Equals("Double"))
{
par = new OracleParameter(name, OracleDbType.Double);
par.Value = Convert.ToInt32(value);
array.Add(par);
}
else if (Pname.Equals("DateTime"))
{
par = new OracleParameter(name, OracleDbType.Date);
string obj = value;
if (!obj.Equals("0001/1/1 0:00:00"))
{
par.Value = Convert.ToDateTime(value);
}
else
{
par.Value = DBNull.Value;
}
array.Add(par);
}
}
#region
//Files += item.Name + ",";
//Values += ":" + item.Name + ",";

//if (item.PropertyType.Name.Equals("String"))
//{
// par = new OracleParameter(item.Name, OracleDbType.Varchar2);
// par.Value = item.GetValue(entity, null) + "";
// array.Add(par);
//}
//else if (item.PropertyType.Name.Equals("Int32"))
//{
// par = new OracleParameter(item.Name, OracleDbType.Int32);
// int temp = Convert.ToInt32(item.GetValue(entity, null));
// if (temp <= -1)
// {
// par.Value = DBNull.Value;
// }
// else
// {
// par.Value = temp;
// }
// array.Add(par);
//}
//else if (item.PropertyType.Name.Equals("Decimal"))
//{
// par = new OracleParameter(item.Name, OracleDbType.Decimal);
// par.Value = Convert.ToDecimal(item.GetValue(entity, null));
// array.Add(par);
//}
//else if (item.PropertyType.Name.Equals("Double"))
//{
// par = new OracleParameter(item.Name, OracleDbType.Double);
// par.Value = Convert.ToInt32(item.GetValue(entity, null));
// array.Add(par);
//}
//else if (item.PropertyType.Name.Equals("DateTime"))
//{
// par = new OracleParameter(item.Name, OracleDbType.Date);
// string obj = item.GetValue(entity, null).ToString();
// if (!obj.Equals("0001/1/1 0:00:00"))
// {
// par.Value = Convert.ToDateTime(item.GetValue(entity, null));
// }
// else
// {
// par.Value = DBNull.Value;
// }
// array.Add(par);
//}
#endregion
}
if (!String.IsNullOrEmpty(Files))
{
Files = Files.Substring(0, Files.Length - 1);
}
if (!String.IsNullOrEmpty(Values))
{
Values = Values.Substring(0, Values.Length - 1);
}

//strSql += Files + ") values(" + Values + ")";

strSql = string.Format(strSql, tableName, Files, Values);

return strSql;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion

#region 获取根据主键查询总记录数的语句
/// <summary>
/// 获取根据主键查询总记录数的语句
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static string GetCountById(T entity)
{
try
{
array = new ArrayList();
Type type = entity.GetType();
PropertyInfo[] p = type.GetProperties();

//表名
string tableName = type.Name;
foreach (Object attr in type.GetCustomAttributes(false))// 遍历该属性的所有注解
{
// 判断是否有属性修饰,并判断属性值
if (attr is TableAttribute)
{
TableAttribute currAttribute = attr as TableAttribute;
if (!string.IsNullOrEmpty(currAttribute.TableName))
{
tableName = currAttribute.TableName;
}
}
}
//sql语句
string strSql = "select count(*) from " + tableName + " where ";

//主键(用来做update的where条件)
OracleParameter parId = null;

foreach (PropertyInfo item in p)
{
//当前字段值
string value = item.GetValue(entity, null) + "";
string name = item.Name;

foreach (Object attr in item.GetCustomAttributes(false))// 遍历该属性的所有注解
{
// 判断当前字段是否是主键
if (attr is PrimaryKeyAttribute)
{
PrimaryKeyAttribute currAttribute = attr as PrimaryKeyAttribute;
if (!string.IsNullOrEmpty(currAttribute.Id))
{
name = currAttribute.Id;
}
if (item.PropertyType.Name.Equals("String"))
{
strSql += name + "='" + value + "'";
}
else
{
strSql += name + "=" + value;
}

if (item.PropertyType.Name.Equals("Int32"))
{
parId = new OracleParameter(name, OracleDbType.Int32);
parId.Value = Convert.ToInt32(value);
}
else if (item.PropertyType.Name.Equals("String"))
{
parId = new OracleParameter(name, OracleDbType.Varchar2);
parId.Value = value;
}
}
}
//---------------------end---------------------
}

if (parId != null)
{
array.Add(parId);
return strSql;
}
else
{
return "";
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion

#region 获取Update语句
/// <summary>
/// 获取Update语句
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static string Update(T entity)
{
try
{
array = new ArrayList();
Type type = entity.GetType();
PropertyInfo[] p = type.GetProperties();

//表名
string tableName = type.Name;
foreach (Object attr in type.GetCustomAttributes(false))// 遍历该属性的所有注解
{
// 判断是否有属性修饰,并判断属性值
if (attr is TableAttribute)
{
TableAttribute currAttribute = attr as TableAttribute;
if (!string.IsNullOrEmpty(currAttribute.TableName))
{
tableName = currAttribute.TableName;
}
}
}
//sql语句
string strSql = "Update " + tableName + " set ";

//字段名称列表,以逗号隔开
string Files = "";

string sqlWhere = "";
//主键(用来做update的where条件)
OracleParameter parId = null;

foreach (PropertyInfo item in p)
{
//------------update后面的where条件------------
OracleParameter par = null;
//当前字段值
string value = item.GetValue(entity, null) + "";
string name = item.Name;

foreach (Object attr in item.GetCustomAttributes(false))// 遍历该属性的所有注解
{
// 判断当前字段是否是数据库字段
if (attr is FieldAttribute)
{
FieldAttribute fileld = attr as FieldAttribute;

if (!string.IsNullOrEmpty(fileld.Field))
{
name = fileld.Field;
}
Files += name + "=:" + name + ",";

if (item.PropertyType.Name.Equals("String"))
{
par = new OracleParameter(name, OracleDbType.Varchar2);
par.Value = value;
array.Add(par);
}
else if (item.PropertyType.Name.Equals("Int32"))
{
par = new OracleParameter(name, OracleDbType.Int32);
int temp = Convert.ToInt32(value);
if (temp <= -1)
{
par.Value = DBNull.Value;
}
else
{
par.Value = temp;
}
array.Add(par);
}
else if (item.PropertyType.Name.Equals("Decimal"))
{
par = new OracleParameter(name, OracleDbType.Decimal);
decimal temp = Convert.ToDecimal(value);
if (temp <= -1)
{
par.Value = DBNull.Value;
}
else
{
par.Value = temp;
}
array.Add(par);
}
else if (item.PropertyType.Name.Equals("Double"))
{
par = new OracleParameter(name, OracleDbType.Double);
par.Value = Convert.ToInt32(value);
array.Add(par);
}
else if (item.PropertyType.Name.Equals("DateTime"))
{
par = new OracleParameter(name, OracleDbType.Date);
string obj = value;
if (!obj.Equals("0001/1/1 0:00:00"))
{
par.Value = Convert.ToDateTime(value);
}
else
{
par.Value = DBNull.Value;
}
array.Add(par);
}
}
// 判断当前字段是否是主键
else if (attr is PrimaryKeyAttribute)
{
PrimaryKeyAttribute currAttribute = attr as PrimaryKeyAttribute;
if (!string.IsNullOrEmpty(currAttribute.Id))
{
name = currAttribute.Id;
}
sqlWhere += " Where " + name + "=:" + name;

if (item.PropertyType.Name.Equals("Int32"))
{
parId = new OracleParameter(name, OracleDbType.Int32);
parId.Value = Convert.ToInt32(value);
}
else if (item.PropertyType.Name.Equals("Decimal"))
{
parId = new OracleParameter(name, OracleDbType.Decimal);
parId.Value = Convert.ToDecimal(value);
}
else if (item.PropertyType.Name.Equals("String"))
{
parId = new OracleParameter(name, OracleDbType.Varchar2);
parId.Value = value;
}
}
}
//---------------------end---------------------
}
if (!String.IsNullOrEmpty(Files))
{
Files = Files.Substring(0, Files.Length - 1);
}

strSql += Files + sqlWhere;

if (parId != null)
{
array.Add(parId);
return strSql;
}
else
{
return "";
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
}

使用:

自动生成sql的更多相关文章

  1. 使用Java注解开发自动生成SQL

    使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...

  2. 使用Excel自动生成sql语句

    在近一段日子里,进入了新的项目组,由于项目需要,经常要将一些Excel表中的数据导入数据库中,以前并没有过多的接触过数据导入与数据处理,对于我来说比较痛苦,今天下午花了几个小时处理数据,但是同事给我提 ...

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

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

  4. springboot+mybatis+mysql 利用mybatis自动生成sql语句

    工具和环境 idea,mysql,JDK1.8 效果图如下 结构图如下 java resources sql文件 /* Navicat MySQL Data Transfer Source Serve ...

  5. 数据库数据对比自动生成sql

    1.故事背景 有一次迭代步入尾声,提交给用户测试,系统管理员在测试环境中初始了一些数据,然后在上线的时候系统管理员再去正式环境初始这一些数据,然而这次数据太多了,说了一次:”为什么要初始化两次?“ 你 ...

  6. 使用sqlmetal工具自动生成SQL数据库的Linq类文件

    第一部:找到sqlmetal.exe. 运行cmd. 执行命令 cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5 ...

  7. Mybatis-Plus BaseMapper自动生成SQL及MapperProxy

    目录 Spring+Mybatis + Mybatis-Plus 自定义无XML的sql生成及MapperProxy代理生成 问题产生背景 框架是如何使用 无Xml的SQL是如何生成生成及SQL长成什 ...

  8. Excel 提供数据 更新或者插入数据 通过函数 自动生成SQL语句

    excel 更新数据 ="UPDATE dbo.yt_vehicleExtensionBase SET yt_purchase_date='"&B2&"' ...

  9. 打开powerDesigner时,创建table对应的自动生成sql语句没有注释

    在创建pdm时由于需要在name列填写的是以后要在表中创建的注释信息,comment中则写的说明信息字数比较多.默认情况下在生成建表sql时不能将name生成注释信息,进行如下设置可以讲name生成注 ...

  10. 根据DELTA自动生成SQL语句

    上传客户端的CLIENTDATASET.delta到服务器的clientdataset.data,服务端解析clientdataset的数据生成相应的SQL语句. 相对于直接调用datasetprov ...

随机推荐

  1. AtCoder Grand Contest #026 C - String Coloring

    Time Limit: 3 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement You are given a st ...

  2. RMI RPC socket

      1.RPC RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC不依赖于具体的 ...

  3. 有趣的Javascript:只需一个JS让万恶的IE5、IE6、IE7、IE8全都支持H5原生Canvas绘图(有演示demo)

    该demo支持IE5以上任意内核的浏览器 查看演示demo:支持IE5以上版本的浏览器Canvas绘图demo 补充:chats.js和echarts等图表库也可以使用本方法兼容IE6以上浏览器 1. ...

  4. this在方法赋值过程中无法保持(隐式丢失)

    在看<高级程序设计>(我的红宝书) P.183页时遇到下面一个问题 var name = "77"; var obj = { name: "88", ...

  5. python2和python3中的range区别

    python2中的range返回的是一个列表 python3中的range返回的是一个迭代值 for i in range(1,10)在python2和python3中都可以使用,但是要生成1-10的 ...

  6. SQL一次性插入大量数据【转载】

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  7. java枚举类型的优势在哪里?--一个实例

    最近在做一个项目,其中涉及到一组操作,命名为: 1. "add"; 2. "logicDel" 3. "physicDel" 4. &quo ...

  8. Redux API之combineReducers

    combineReducers(reducers) 随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分. combineReducers 辅助函 ...

  9. STL::next_permutation();

    next_permutation()可以按字典序生成所给区间的全排列. 在STL中,除了next_permutation()外,还有一个函数prev_permutation(),两者都是用来计算排列组 ...

  10. 虚拟机出现ping DUP

    在主机的网络连接里,停用虚拟网卡vmnet1和vmnet8,再启用虚拟网卡vmnet1和vmnet8.