大恶人吉日嘎拉之走火入魔闭门造车之.NET疯狂架构经验分享系列之(十三)代码生成器
2009-09-29 13:24 通用C#系统架构 阅读(5854) 评论(29) 编辑 收藏 举报
程序不在于多在于精,今天不讲废话了,本文适合初学者参考阅读,请直接看代码生成器设计思路整理如下图解:
光是靠文字描述很难讲清楚事情,大家看图,就容易明白了,很直观的表现在我们眼前了。
整个架设思路分的4个部分:
A:底层物理数据库层,主要是存储数据用的。
B:数据库访问层,主要是为了写一套代码可以跑在多种数据库上。
C:一些辅助工具、基础组件,是为了加强自动产生代码的功能、简化代码复杂性,进行合理的分工协作用的。
D:自动生成的代码部分,用PowerDesigner设计简洁明了、易于沟通理解,代码分自动生成及人工部分,为了重复生成方便。
详细功能部分只要是分12个部分来讲解,分别如下:
01:我业务里需要的表,是自己的业务里建立的,不大可能有个万能的表,能把所有的业务都能覆盖了。
02:这是系统里通用的一些表,例如序号生成器表、记录当前的序号使用情况,有点儿像Oracle的序列。
03:这个是一个数据库访问层的基础类,其他几个类都继承了这个类,在这个类的基础上进行个性化扩展,重复的代码会少一些。
04:为了实现不同数据库的个性化功能,每个具体数据库采用了个性化的实现,例如获取当前时间,各种数据库都有些不一样。
05:常用的数据库操作,都封装在这个类里了,其实我们天天在做的就是添加、删除、修改、查询而已,没啥了不起的。
06:这个是我的基类,里面有很多功能都封装好了,别的类继承一下,就会有很多功能自动就有了,不用所有的代码都要生成,共性的代码,基础类里写就可以了。
07:SQL语句人工写太烦恼了,就算用生成器写也不太好,数据库表名字段名经常若有变动,程序改来改去很烦恼,而且测试也不太好测试,用生成器的好处就是在在编译阶段发现错误,将错误扼杀在编译阶段。
08:是我的序号生成器、排序码生成器,数据都需要排序功能的,先输入的数据排在前面?还是后输入的数据排在前面?还是我人工需要干预数据的排序顺序?有生成器生成排序码、然后接着想怎么处理都方便了,可以满足个性化的数据排序需要了。
09:PowerDesigner是非常优秀的数据表结构设计工具,可以设计多种数据库的表结构,也算是业界的一个标准一样了,我们的代码生成器直接能读懂你的设计文档,也挺有意思的,比读取数据里的表感觉档次高一些,毕竟我们是专业的IT软件人员嘛。
10:这是自己写的代码生成器,很简单,很原始,自己想怎么修改就怎么修改,生成的代码,还有注释呢,你自己想扩展也可以的,总共也就1000行代码而已,读几次就全明白了。
11:这里是代码生成器目前可以生成的代码,以后精力充沛了,可以把页面的代码页生成生成,服务层的代码也可以生成,就看有没有时间去折腾了,或者页面层、服务层的代码通过复制粘贴标准例子程序来搞定,也很方便的,未必代码生成器就强多少。
12:个性化的业务逻辑还是需要自己写sql语句实现,这时候代码就不要放在一起,我设计变了,整个代码可以重新生成,但是别覆盖我人工写的代码部分,这部分被覆盖了,损失还是很惨重的了,不能因为设计变了,重新生成了代码,自己辛苦写的程序都丢了吧。
补充一句:胳膊扭不过大腿,动软的代码生成器,大家用得很多制作也精良,我这个当然不能盲目自己走自己的,只能要考虑与其他人的兼容,我的架构是与动软的兼容的,不冲突的,你也可以用我的代码生成,也可以用动软的,都可以,我的可以做个参考,比较比较。
代码参考
光是靠文字描述很难讲清楚事情,大家看图,就容易明白了,很直观的表现在我们眼前了。
整个架设思路分的4个部分:
A:底层物理数据库层,主要是存储数据用的。
B:数据库访问层,主要是为了写一套代码可以跑在多种数据库上。
C:一些辅助工具、基础组件,是为了加强自动产生代码的功能、简化代码复杂性,进行合理的分工协作用的。
D:自动生成的代码部分,用PowerDesigner设计简洁明了、易于沟通理解,代码分自动生成及人工部分,为了重复生成方便。
详细功能部分只要是分12个部分来讲解,分别如下:
01:我业务里需要的表,是自己的业务里建立的,不大可能有个万能的表,能把所有的业务都能覆盖了。
02:这是系统里通用的一些表,例如序号生成器表、记录当前的序号使用情况,有点儿像Oracle的序列。
03:这个是一个数据库访问层的基础类,其他几个类都继承了这个类,在这个类的基础上进行个性化扩展,重复的代码会少一些。
04:为了实现不同数据库的个性化功能,每个具体数据库采用了个性化的实现,例如获取当前时间,各种数据库都有些不一样。
05:常用的数据库操作,都封装在这个类里了,其实我们天天在做的就是添加、删除、修改、查询而已,没啥了不起的。
06:这个是我的基类,里面有很多功能都封装好了,别的类继承一下,就会有很多功能自动就有了,不用所有的代码都要生成,共性的代码,基础类里写就可以了。
07:SQL语句人工写太烦恼了,就算用生成器写也不太好,数据库表名字段名经常若有变动,程序改来改去很烦恼,而且测试也不太好测试,用生成器的好处就是在在编译阶段发现错误,将错误扼杀在编译阶段。
08:是我的序号生成器、排序码生成器,数据都需要排序功能的,先输入的数据排在前面?还是后输入的数据排在前面?还是我人工需要干预数据的排序顺序?有生成器生成排序码、然后接着想怎么处理都方便了,可以满足个性化的数据排序需要了。
09:PowerDesigner是非常优秀的数据表结构设计工具,可以设计多种数据库的表结构,也算是业界的一个标准一样了,我们的代码生成器直接能读懂你的设计文档,也挺有意思的,比读取数据里的表感觉档次高一些,毕竟我们是专业的IT软件人员嘛。
10:这是自己写的代码生成器,很简单,很原始,自己想怎么修改就怎么修改,生成的代码,还有注释呢,你自己想扩展也可以的,总共也就1000行代码而已,读几次就全明白了。
11:这里是代码生成器目前可以生成的代码,以后精力充沛了,可以把页面的代码页生成生成,服务层的代码也可以生成,就看有没有时间去折腾了,或者页面层、服务层的代码通过复制粘贴标准例子程序来搞定,也很方便的,未必代码生成器就强多少。
12:个性化的业务逻辑还是需要自己写sql语句实现,这时候代码就不要放在一起,我设计变了,整个代码可以重新生成,但是别覆盖我人工写的代码部分,这部分被覆盖了,损失还是很惨重的了,不能因为设计变了,重新生成了代码,自己辛苦写的程序都丢了吧。
补充一句:胳膊扭不过大腿,动软的代码生成器,大家用得很多制作也精良,我这个当然不能盲目自己走自己的,只能要考虑与其他人的兼容,我的架构是与动软的兼容的,不冲突的,你也可以用我的代码生成,也可以用动软的,都可以,我的可以做个参考,比较比较。
代码参考
Code
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2009 , Jirisoft , Ltd.
//------------------------------------------------------------
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace DotNet.Common.Business
{
using DotNet.Common.Utilities;
using DotNet.Common.DbUtilities;
/// <summary>
/// DbLogic
/// 通用基类
///
/// 这个类可是修改了很多次啊,已经比较经典了,随着专业的提升,人也会不断提高,技术也会越来越精湛。
///
/// 修改纪录
///
/// 2009.01.15 版本:4.7 JiRiGaLa 将方法修改为 static 静态的,提高运行速度。
/// 2009.01.05 版本:4.6 JiRiGaLa MySql 获取前几个的错误进行修正。
/// 2008.06.03 版本:4.5 JiRiGaLa IN SQL语句调试、修改错误。
/// 2008.05.31 版本:4.4 JiRiGaLa 改进 参数名param去掉了,变量名首字母小写了。
/// 2008.05.09 版本:4.3 JiRiGaLa 改进 树型结构的数据的获得函数部分。
/// 2007.11.08 版本:4.2 JiRiGaLa 改进 DataTableToStringList 为 FieldToList。
/// 2007.11.05 版本:4.1 JiRiGaLa 改进 GetDS、GetDT 功能,整体思路又上一个台阶,基类的又一次飞跃。
/// 2007.11.05 版本:4.0 JiRiGaLa 改进 支持不是“ID”为字段的主键表。
/// 2007.11.01 版本:3.9 JiRiGaLa 改进 BUOperatorInfo 去掉这个变量,可以提高性能,提高速度,基类的又一次飞跃。
/// 2007.09.13 版本:3.8 JiRiGaLa 改进 BUBaseBusinessLogic.SQLLogicConditional 错误。
/// 2007.08.14 版本:3.7 JiRiGaLa 改进 WebService 模式下 DataSet 传输数据的速度问题。
/// 2007.07.20 版本:3.6 JiRiGaLa 改进 DataSet 修改为 DataTable 应该能提高一些速度吧。
/// 2007.05.20 版本:3.5 JiRiGaLa 改进 GetList() 方法整理,这次又应该是一次升华,质的飞跃很不容易啊,身心都有提高了。
/// 2007.05.20 版本:3.4 JiRiGaLa 改进 Exists() 方法整理。
/// 2007.05.13 版本:3.3 JiRiGaLa 改进 GetProperty(),SetProperty(),Delete() 方法整理。
/// 2007.05.10 版本:3.2 JiRiGaLa 改进 GetList() 方法整理。
/// 2007.04.10 版本:3.1 JiRiGaLa 添加 GetNextID,GetPreviousID 方法整理。
/// 2007.03.03 版本:3.0 JiRiGaLa 进行了一次彻底的优化工作,方法的位置及功能整理。
/// 2007.03.01 版本:2.0 JiRiGaLa 重新调整代码的规范化。
/// 2006.02.05 版本:1.1 JiRiGaLa 重新调整代码的规范化。
/// 2005.12.30 版本:1.0 JiRiGaLa 数据库连接方式都进行改进
/// 2005.09.04 版本:1.0 JiRiGaLa 执行数据库脚本
/// 2005.08.19 版本:1.0 JiRiGaLa 整理一下编排
/// 2005.07.10 版本:1.0 JiRiGaLa 修改了程序,格式以及理念都有些提高,应该是一次大突破
/// 2004.11.12 版本:1.0 JiRiGaLa 添加了最新的GetParent、GetChildren、GetParentChildren 方法
/// 2004.07.21 版本:1.0 JiRiGaLa UpdateRecord、Delete、SetProperty、GetProperty、ExecuteNonQuery、GetRecord 方法进行改进。
/// 还删除一些重复的代码,提取了最优化的方法,有时候写的代码真的是垃圾,可能自己也没有注意时就写出了垃圾。
/// GetRepeat、GetDayOfWeek、ExecuteProcedure、GetFromProcedure 方法进行改进,基本上把所有的方法都重新写了一遍。
///
/// 版本:4.7
///
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2009.01.15</date>
/// </author>
/// </summary>
public class DbLogic
{
#region public static String SqlSafe(String value) 检查参数的安全性
/// <summary>
/// 检查参数的安全性
/// </summary>
/// <param name="value">参数</param>
/// <returns>安全的参数</returns>
public static String SqlSafe(String value)
{
value = value.Replace("'", "''");
// value = value.Replace("%", "'%");
return value;
}
#endregion
#region public static String GetWhereString(IDbHelper dbHelper, String[] names, ref Object[] values, String relation) 获得条件语句
/// <summary>
/// 获得条件语句
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="relation">逻辑关系</param>
/// <returns>字符串</returns>
public static String GetWhereString(IDbHelper dbHelper, String[] names, ref Object[] values, String relation)
{
String returnValue = String.Empty;
String subSqlQuery = String.Empty;
for (int i = 0; i < names.Length; i++)
{
if ((names[i] != null) && (names[i].Length > 0))
{
//if (values[i] == null || String.IsNullOrEmpty(values[i].ToString()))
if (values[i] == null)
{
subSqlQuery = " (" + names[i] + " IS NULL) ";
}
else
{
// 这里操作,就会有些重复了,不应该进行处理
// values[i] = this.SqlSafe(values[i].ToString());
subSqlQuery = " (" + names[i] + " = " + dbHelper.GetParameter(names[i]) + ") ";
if ((values[i].ToString().IndexOf('[') >= 0) || (values[i].ToString().IndexOf(']') >= 0))
{
values[i] = values[i].ToString().Replace("[", "/[");
values[i] = values[i].ToString().Replace("]", "/]");
values[i] = SqlSafe(values[i].ToString());
subSqlQuery = " (" + names[i] + " LIKE '" + values[i] + "' ESCAPE '/') ";
values[i] = null;
// subSqlQuery = " (" + names[i] + " LIKE ? ESCAPE '/') ";
}
}
returnValue += subSqlQuery + relation;
}
}
if (returnValue.Length > 0)
{
returnValue = returnValue.Substring(0, returnValue.Length - relation.Length - 1);
}
return returnValue;
}
#endregion
//
// 对数据库进行操作的方法
//
#region public static String ExecuteScalar(IDbHelper dbHelper, String sqlQuery) 执行SQL查询
/// <summary>
/// 执行SQL查询
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name=sqlQuery>查询语句</param>
/// <returns>字符串</returns>
public static String ExecuteScalar(IDbHelper dbHelper, String sqlQuery)
{
String returnValue = String.Empty;
Object returnObject = dbHelper.ExecuteScalar(sqlQuery);
if (returnObject != null)
{
returnValue = returnObject.ToString();
}
return returnValue;
}
#endregion
#region public static int ExecuteNonQuery(IDbHelper dbHelper, String sqlQuery) 执行SQL查询
/// <summary>
/// 执行SQL查询
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="sqlQuery">sql查询</param>
/// <returns>影响行数</returns>
public static int ExecuteNonQuery(IDbHelper dbHelper, String sqlQuery)
{
return dbHelper.ExecuteNonQuery(sqlQuery);
}
#endregion
#region public static int ExecuteNonQuery(IDbHelper dbHelper, String[] sqlQuerys) 执行SQL数组查询
/// <summary>
/// 执行SQL数组查询
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="sqlQuerys">查询语句数组</param>
/// <returns>影响行数</returns>
public static int ExecuteNonQuery(IDbHelper dbHelper, String[] sqlQuerys)
{
int returnValue = 0;
String sqlQuery = String.Empty;
for (int i = 0; i < sqlQuerys.Length; i++)
{
sqlQuery = sqlQuerys[i];
// 执行SQL查询
returnValue += ExecuteNonQuery(dbHelper, sqlQuery);
}
return returnValue;
}
#endregion
//
// 读取属性部分
//
#region public static bool IsModifed(IDbHelper dbHelper, String tableName, String id, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">表名</param>
/// <param name="id">代码</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
public static bool IsModifed(IDbHelper dbHelper, String tableName, String id, String oldModifyUserID, String oldModifyDate)
{
return IsModifed(dbHelper, tableName, BaseBusinessLogic.FieldID, id, oldModifyUserID, oldModifyDate);
}
#endregion
#region public static bool IsModifed(IDbHelper dbHelper, String tableName, String fieldName, String fieldValue, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">表名</param>
/// <param name="id">代码</param>
/// <param name="fieldName">字段</param>
/// <param name="fieldValue">值</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
public static bool IsModifed(IDbHelper dbHelper, String tableName, String fieldName, String fieldValue, String oldModifyUserID, String oldModifyDate)
{
bool returnValue = false;
DataTable dataTable = new DataTable(tableName);
String sqlQuery = " SELECT " + BaseBusinessLogic.FieldID
+ "," + BaseBusinessLogic.FieldCreateUserID
+ "," + BaseBusinessLogic.FieldCreateDate
+ "," + BaseBusinessLogic.FieldModifyUserID
+ "," + BaseBusinessLogic.FieldModifyDate
+ " FROM " + tableName
+ " WHERE " + fieldName + " = " + dbHelper.GetParameter(fieldName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(fieldName, fieldValue));
returnValue = IsModifed(dataTable, oldModifyUserID, oldModifyDate);
return returnValue;
}
#endregion
#region private static bool IsModifed(DataTable dataTable, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dataTable">数据表</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
private static bool IsModifed(DataTable dataTable, String oldModifyUserID, String oldModifyDate)
{
bool returnValue = false;
foreach (DataRow dataRow in dataTable.Rows)
{
returnValue = IsModifed(dataRow, oldModifyUserID, oldModifyDate);
}
return returnValue;
}
#endregion
#region public static bool IsModifed(DataRow dataRow, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dataRow">数据行</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
public static bool IsModifed(DataRow dataRow, String oldModifyUserID, String oldModifyDate)
{
bool returnValue = false;
if ((dataRow[BaseBusinessLogic.FieldModifyUserID] == DBNull.Value) || ((dataRow[BaseBusinessLogic.FieldModifyDate] == DBNull.Value)))
{
returnValue = false;
return returnValue;
}
if ((!dataRow[BaseBusinessLogic.FieldModifyUserID].ToString().Equals(oldModifyUserID)) || (!dataRow[BaseBusinessLogic.FieldModifyDate].ToString().Equals(oldModifyDate)))
{
returnValue = true;
return returnValue;
}
return returnValue;
}
#endregion
#region public static String GetProperty(IDbHelper dbHelper, String tableName, String name, String value, String targetField) 读取属性
/// <summary>
/// 读取属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">键值</param>
/// <param name="targetField">获取字段</param>
/// <returns>属性</returns>
public static String GetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String targetField)
{
String returnValue = String.Empty;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
returnValue = GetProperty(dbHelper, tableName, names, values, targetField);
return returnValue;
}
#endregion
#region public static String GetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField) 读取属性
/// <summary>
/// 读取属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="targetField">获取字段</param>
/// <returns>String</returns>
public static String GetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField)
{
String returnValue = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
returnValue = GetProperty(dbHelper, tableName, names, values, targetField);
return returnValue;
}
#endregion
#region public static String GetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField) 读取属性
/// <summary>
/// 读取属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>属性</returns>
public static String GetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField)
{
String returnValue = String.Empty;
String sqlQuery = " SELECT " + targetField
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = returnObject.ToString();
}
return returnValue;
}
#endregion
#region public static String FindID(IDbHelper dbHelper, String tableName, String name, Object value) 获得ID属性
/// <summary>
/// 获得ID属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">键值</param>
/// <returns>属性</returns>
public static String FindID(IDbHelper dbHelper, String tableName, String name, Object value)
{
return GetProperty(dbHelper, tableName, name, value, BaseBusinessLogic.FieldID);
}
#endregion
#region public static String FindID(IDbHelper dbHelper, String tableName, String name1, String value1, String name2, String value2) 获得ID属性
/// <summary>
/// 获得ID属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>String</returns>
public static String FindID(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String returnValue = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
returnValue = GetProperty(dbHelper, tableName, names, values, BaseBusinessLogic.FieldID);
return returnValue;
}
#endregion
#region public static String FindID(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 获得ID属性
/// <summary>
/// 获得ID属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>属性</returns>
public static String FindID(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
return GetProperty(dbHelper, tableName, names, values, BaseBusinessLogic.FieldID);
}
#endregion
//
// 设置属性部分
//
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">条件字段</param>
/// <param name="value">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
// sqlBuilder.SetDBNow(FieldModifyDate);
sqlBuilder.SetWhere(name, value);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">条件字段</param>
/// <param name="value1">条件值</param>
/// <param name="name2">条件字段</param>
/// <param name="value2">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
// sqlBuilder.SetDBNow(FieldModifyDate);
sqlBuilder.SetWhere(name1, value1);
sqlBuilder.SetWhere(name2, value2);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">条件字段</param>
/// <param name="values">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField, Object targetValue)
{
int returnValue = 0;
for (int i = 0; i < values.Length; i++)
{
returnValue += SetProperty(dbHelper, tableName, name, values[i], targetField, targetValue);
}
return returnValue;
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String[] targetFields, Object[] targetValues) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">条件字段</param>
/// <param name="value">条件值</param>
/// <param name="targetFields">更新字段</param>
/// <param name="targetValues">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String[] targetFields, Object[] targetValues)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
for (int i = 0; i < targetFields.Length; i++)
{
sqlBuilder.SetValue(targetFields[i], targetValues[i]);
}
sqlBuilder.SetDBNow(BaseBusinessLogic.FieldModifyDate);
sqlBuilder.SetWhere(name, value);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">条件字段</param>
/// <param name="values">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
sqlBuilder.SetDBNow(BaseBusinessLogic.FieldModifyDate);
sqlBuilder.SetWhere(names, values);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int UpdateRecord(IDbHelper dbHelper, String tableName, String name, String value, String targetField, Object targetValue) 更新记录
/// <summary>
/// 更新记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">代码</param>
/// <param name="value">代码值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int UpdateRecord(IDbHelper dbHelper, String tableName, String name, String value, String targetField, Object targetValue)
{
int returnValue = 0;
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
sqlBuilder.SetWhere(name, value);
returnValue = sqlBuilder.EndUpdate();
return returnValue;
}
#endregion
//
// 获取ID部分 获得代码数组 String[] 常用
//
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value) 读取列表部分
/// <summary>
/// 读取列表部分
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values)
{
return GetIDs(dbHelper, tableName, name, values, BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <param name="targetField">目标字段</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField)
{
DataTable dataTable = new DataTable(tableName);
String sqlQuery = " SELECT " + targetField
+ " FROM " + tableName
+ " WHERE " + name + " IN (" + BaseBusinessLogic.ObjectsToList(values) + ")";
dbHelper.Fill(dataTable, sqlQuery);
return BaseBusinessLogic.FieldToArray(dataTable, targetField);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField)
{
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField, int topLimit)
{
String[] names = new String[0];
Object[] values = new Object[0];
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="targetField">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, int topLimit)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
int topLimit = 0;
String order = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="targetField">排序</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField)
{
int topLimit = 0;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="targetField">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, int topLimit)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <param name="targetField">目标字段</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField)
{
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
String fillTableName = tableName;
String order = String.Empty;
int topLimit = 0;
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据集</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField)
{
int topLimit = 0;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, int topLimit) 获取数据集
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="targetField">目标字段</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, int topLimit)
{
if (string.IsNullOrEmpty(targetField))
{
targetField = BaseBusinessLogic.FieldID;
}
// 这里是需要完善的功能,完善了这个,是一次重大突破
String sqlQuery = " SELECT " + targetField + " FROM " + tableName;
String whereSql = String.Empty;
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery = " SELECT TOP " + topLimit.ToString() + targetField + " FROM " + tableName;
break;
case DataBaseType.Oracle:
whereSql = " ROWNUM < = " + topLimit;
break;
}
}
String subSql = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
if (subSql.Length > 0)
{
if (whereSql.Length > 0)
{
whereSql = whereSql + BaseBusinessLogic.SQLLogicConditional + subSql;
}
else
{
whereSql = subSql;
}
}
if (whereSql.Length > 0)
{
sqlQuery += " WHERE " + whereSql;
}
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.MySql:
sqlQuery += " LIMIT 0, " + topLimit;
break;
}
}
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return BaseBusinessLogic.FieldToArray(dataTable, targetField);
}
#endregion
//
// 读取列表部分 填充DataTable 常用
//
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value) 读取列表部分
/// <summary>
/// 读取列表部分
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values)
{
return GetDT(dbHelper, tableName, name, values, String.Empty);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order)
{
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="ids">代码数组</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids)
{
return GetDT(dbHelper, tableName, BaseBusinessLogic.FieldID, ids);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="ids">代码数组</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids, String order)
{
return GetDT(dbHelper, tableName, BaseBusinessLogic.FieldID, ids, order);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order, int topLimit)
{
String[] names = new String[0];
Object[] values = new Object[0];
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order, int topLimit)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
int topLimit = 0;
String order = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order)
{
int topLimit = 0;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order, int topLimit)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order)
{
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values, String order)
{
DataTable dataTable = new DataTable(tableName);
String sqlQuery = " SELECT * "
+ " FROM " + tableName
+ " WHERE " + name + " IN (" + BaseBusinessLogic.ObjectsToList(values) + ")";
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
dbHelper.Fill(dataTable, sqlQuery);
return dataTable;
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
String fillTableName = tableName;
String order = String.Empty;
int topLimit = 0;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据集</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order)
{
int topLimit = 0;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order, int topLimit) 获取数据集
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order, int topLimit)
{
// 这里是需要完善的功能,完善了这个,是一次重大突破
String sqlQuery = " SELECT * FROM " + tableName;
String whereSql = String.Empty;
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery = " SELECT TOP " + topLimit.ToString() + " * FROM " + tableName;
break;
case DataBaseType.Oracle:
whereSql = " ROWNUM < = " + topLimit;
break;
}
}
String subSql = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
if (subSql.Length > 0)
{
if (whereSql.Length > 0)
{
whereSql = whereSql + BaseBusinessLogic.SQLLogicConditional + subSql;
}
else
{
whereSql = subSql;
}
}
if (whereSql.Length > 0)
{
sqlQuery += " WHERE " + whereSql;
}
if ((order != null) && (order.Length > 0))
{
sqlQuery += " ORDER BY " + order;
}
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.MySql:
sqlQuery += " LIMIT 0, " + topLimit;
break;
}
}
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
//
// 读取列表部分 不常用
//
#region public static DataTable GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName) 通过存储过程获取表数据
/// <summary>
/// 通过存储过程获取表数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="procedureName">存储过程名</param>
/// <param name="tableName">填充表</param>
/// <returns>数据集</returns>
public static DataTable GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName)
{
return dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, null);
}
#endregion
#region public static DataSet GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName, String id) 通过存储过程获取表数据
/// <summary>
/// 通过存储过程获取表数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="procedureName">存储过程名</param>
/// <param name="tableName">填充表</param>
/// <param name="id">代码值</param>
/// <returns>数据集</returns>
public static DataTable GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName, String id)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = BaseBusinessLogic.FieldID;
values[0] = id;
return dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, dbHelper.MakeParameters(names, values));
}
#endregion
//
// 记录是否存在
//
#region public static bool Exists(IDbHelper dbHelper, String tableName, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="id">字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String id)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = BaseBusinessLogic.FieldID;
values[0] = id;
return Exists(dbHelper, tableName, names, values);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return Exists(dbHelper, tableName, names, values);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String[] ids) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="ids">字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String[] ids)
{
bool returnValue = false;
for (int i = 0; i < ids.Length; i++)
{
returnValue = Exists(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
if (returnValue)
{
break;
}
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name, String[] values) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name, String[] values)
{
bool returnValue = false;
for (int i = 0; i < values.Length; i++)
{
returnValue = Exists(dbHelper, tableName, name, values[i]);
if (returnValue)
{
break;
}
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value, String id)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) FROM " + tableName;
if (name == null)
{
sqlQuery += " WHERE (" + name + " IS NULL ) ";
}
else
{
sqlQuery += " WHERE (" + name + " = " + dbHelper.GetParameter(name) + ") ";
}
if (id.Length > 0)
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + BaseBusinessLogic.FieldID + " <> " + dbHelper.GetParameter(BaseBusinessLogic.FieldID) + " ) ";
}
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name;
values[0] = value;
if (id.Length > 0)
{
names[1] = BaseBusinessLogic.FieldID;
values[1] = id;
}
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
return Exists(dbHelper, tableName, names, values);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String name, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="name">目标字段名</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String id)
{
return Exists(dbHelper, tableName, name1, value1, name2, value2, BaseBusinessLogic.FieldID, id);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String name, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="name">目标字段名</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String name, String id)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) FROM " + tableName;
if (value1 == null)
{
sqlQuery += " WHERE (" + name1 + " IS NULL) ";
}
else
{
sqlQuery += " WHERE (" + name1 + " = " + dbHelper.GetParameter(name1) + ") ";
}
if (value2 == null)
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "(" + name2 + " IS NULL) ";
}
else
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "(" + name2 + " = " + dbHelper.GetParameter(name2) + ") ";
}
if (id.Length > 0)
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + name + " <> " + dbHelper.GetParameter(name) + ") ";
}
String[] names = new String[3];
Object[] values = new Object[3];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
if (id.Length > 0)
{
names[2] = name;
values[2] = id;
}
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) "
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String name, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <param name="name">目标字段名</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String name, String id)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) "
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + BaseBusinessLogic.FieldID + " <> '" + id + "' ) ";
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
//
// 删除数据库方法
//
#region public static int Delete(IDbHelper dbHelper, String tableName) 删除表中的所有数据
/// <summary>
/// 删除表中的所有数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName)
{
return BatchDelete(dbHelper, tableName, new String[0], new Object[0]);
}
#endregion
#region public static int Delete(IDbHelper dbHelper, String tableName, String id) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <param name="id">字段值</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName, String id)
{
return Delete(dbHelper, tableName, BaseBusinessLogic.FieldID, id);
}
#endregion
#region public static int BatchDelete(IDbHelper dbHelper, String tableName, String[] ids) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <param name="ids">字段值</param>
/// <returns>影响行数</returns>
public static int BatchDelete(IDbHelper dbHelper, String tableName, String[] ids)
{
int returnValue = 0;
for (int i = 0; i < ids.Length; i++)
{
returnValue += Delete(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
}
return returnValue;
}
#endregion
#region public static int BatchDelete(IDbHelper dbHelper, String tableName, String name, Object[] values) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <param name="name">目标字段名</param>
/// <param name="values">目标字段值</param>
/// <returns>影响行数</returns>
public static int BatchDelete(IDbHelper dbHelper, String tableName, String name, Object[] values)
{
int returnValue = 0;
for (int i = 0; i < values.Length; i++)
{
returnValue += Delete(dbHelper, tableName, name, values[i]);
}
return returnValue;
}
#endregion
#region public static int Delete(IDbHelper dbHelper, String tableName, String name, Object value) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表格名</param>
/// <param name="name">代码</param>
/// <param name="value">代码值</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName, String name, Object value)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return BatchDelete(dbHelper, tableName, names, values);
}
#endregion
#region public static int Delete(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
return BatchDelete(dbHelper, tableName, names, values);
}
#endregion
#region public int static BatchDelete(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <returns>影响行数</returns>
public static int BatchDelete(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
String sqlQuery = " DELETE " + " FROM " + tableName;
String whereString = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
if (whereString.Length > 0)
{
sqlQuery += " WHERE " + whereString;
}
return dbHelper.ExecuteNonQuery(sqlQuery, dbHelper.MakeParameters(names, values));
}
#endregion
#region public static int Truncate(IDbHelper dbHelper, String tableName) 截断表格数据
/// <summary>
/// 截断表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表格</param>
/// <returns>是否成功</returns>
public static int Truncate(IDbHelper dbHelper, String tableName)
{
String sqlQuery = " Truncate Table " + tableName;
return dbHelper.ExecuteNonQuery(sqlQuery);
}
#endregion
//
// 锁定表记录
//
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String id) 锁定表记录
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="id">字段值</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String id)
{
return LockNoWait(dbHelper, tableName, BaseBusinessLogic.FieldID, id);
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] ids) 锁定表记录
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="ids">字段值</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] ids)
{
int returnValue = 0;
for (int i = 0; i < ids.Length; i++)
{
returnValue += LockNoWait(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
}
return returnValue;
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String name, Object value) 锁定表记录
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="id">代码值</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String name, Object value)
{
int returnValue = 0;
String sqlQuery = " SELECT " + BaseBusinessLogic.FieldID + " FROM " + tableName;
if (name == null)
{
sqlQuery += " WHERE (" + name + " IS NULL ) ";
}
else
{
sqlQuery += " WHERE (" + name + " = " + dbHelper.GetParameter(name) + ")";
}
sqlQuery += " FOR UPDATE NOWAIT ";
try
{
DataSet dataSet = new DataSet();
dbHelper.Fill(dataSet, sqlQuery, "ForUpdateNoWait", dbHelper.MakeParameters(name, value));
returnValue = dataSet.Tables["ForUpdateNoWait"].Rows.Count;
}
catch
{
returnValue = -1;
}
return returnValue;
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 记录是否存在
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
return LockNoWait(dbHelper, tableName, names, values);
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 记录是否存在
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
int returnValue = 0;
String sqlQuery = " SELECT " + BaseBusinessLogic.FieldID
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
sqlQuery += " FOR UPDATE NOWAIT ";
try
{
DataSet dataSet = new DataSet();
dbHelper.Fill(dataSet, sqlQuery, "ForUpdateNoWait", dbHelper.MakeParameters(names, values));
returnValue = dataSet.Tables["ForUpdateNoWait"].Rows.Count;
}
catch
{
returnValue = -1;
}
return returnValue;
}
#endregion
//
// 树型结构的算法
//
#region public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父节点列表
/// <summary>
/// 获取父节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + BaseBusinessLogic.FieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName;
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery += " WHERE (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") ";
break;
case DataBaseType.Oracle:
sqlQuery += " WHERE (SUBSTR(" + dbHelper.GetParameter(fieldCode) + ", 1, LENGTH(" + fieldCode + ")) = " + fieldCode + ") ";
break;
}
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = fieldCode;
values[0] = code;
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order, bool idOnly) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="id">值</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + fieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName
+ " START WITH " + fieldID + " = " + dbHelper.GetParameter(fieldID)
+ " CONNECT BY PRIOR " + fieldID + " = " + fieldParentID;
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[1];
names[0] = fieldID;
Object[] values = new Object[1];
values[0] = id;
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String[] ids, String fieldParentID, String order, bool idOnly) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="ids">代码数组</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String[] ids, String fieldParentID, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + fieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName
+ " START WITH " + fieldID + " IN (" + BaseBusinessLogic.ObjectsToList(ids) + ")"
+ " CONNECT BY PRIOR " + fieldID + " = " + fieldParentID;
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery);
return dataTable;
}
#endregion
#region private static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
private static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + BaseBusinessLogic.FieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName;
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery += " WHERE (LEFT(" + fieldCode + ", LEN(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + " ) ";
break;
case DataBaseType.Oracle:
sqlQuery += " WHERE (SUBSTR(" + fieldCode + ", 1, LENGTH(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + " ) ";
break;
}
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[2];
names[0] = fieldCode;
names[1] = fieldCode;
Object[] values = new Object[2];
values[0] = code;
values[1] = code;
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region private static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly) 获取父子节点列表
/// <summary>
/// 获取父子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
private static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + BaseBusinessLogic.FieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName;
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery += " WHERE (LEFT(" + fieldCode + ", LEN(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + ") ";
sqlQuery += " OR (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") ";
break;
case DataBaseType.Oracle:
sqlQuery += " WHERE (SUBSTR(" + fieldCode + ", 1, LENGTH(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + ") ";
sqlQuery += " OR (" + fieldCode + " = SUBSTR(" + dbHelper.GetParameter(fieldCode) + ", 1, LENGTH(" + fieldCode + "))) ";
break;
}
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[3];
names[0] = fieldCode;
names[1] = fieldCode;
names[2] = fieldCode;
Object[] values = new Object[3];
values[0] = code;
values[1] = code;
values[2] = code;
DataTable dataTable = new DataTable();
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父节点列表
/// <summary>
/// 获取父节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return GetParentsByCode(dbHelper, tableName, fieldCode, code, order, false);
}
#endregion
#region public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="id">值</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order)
{
return GetChildrens(dbHelper, tableName, fieldID, id, fieldParentID, order, false);
}
#endregion
#region public static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return GetChildrensByCode(dbHelper, tableName, fieldCode, code, order, false);
}
#endregion
#region public static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父子节点列表
/// <summary>
/// 获取父子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return GetParentChildrensByCode(dbHelper, tableName, fieldCode, code, order, false);
}
#endregion
#region public static String[] GetParentsIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父节点列表
/// <summary>
/// 获取父节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetParentsIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return BaseBusinessLogic.FieldToArray(GetParentsByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetChildrensID(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="id">值</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetChildrensID(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order)
{
return BaseBusinessLogic.FieldToArray(GetChildrens(dbHelper, tableName, fieldID, id, fieldParentID, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetChildrensByIDCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetChildrensIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return BaseBusinessLogic.FieldToArray(GetChildrensByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetParentChildrensIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父子节点列表
/// <summary>
/// 获取父子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetParentChildrensIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return BaseBusinessLogic.FieldToArray(GetParentChildrensByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String GetParentIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code) 获取父节点
/// <summary>
/// 获取父节点
/// </summary>
/// <param name="tableName">目标表名</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编号</param>
/// <returns>代码</returns>
public static String GetParentIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code)
{
String parentID = String.Empty;
String sqlQuery = " SELECT MAX(ID) AS ID "
+ " FROM " + tableName
+ " WHERE (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") "
+ " AND " + fieldCode + " <> '" + code + " ' ";
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = fieldCode;
values[0] = code;
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
parentID = returnObject.ToString();
}
return parentID;
}
#endregion
}
}
//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2009 , Jirisoft , Ltd.
//------------------------------------------------------------
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
namespace DotNet.Common.Business
{
using DotNet.Common.Utilities;
using DotNet.Common.DbUtilities;
/// <summary>
/// DbLogic
/// 通用基类
///
/// 这个类可是修改了很多次啊,已经比较经典了,随着专业的提升,人也会不断提高,技术也会越来越精湛。
///
/// 修改纪录
///
/// 2009.01.15 版本:4.7 JiRiGaLa 将方法修改为 static 静态的,提高运行速度。
/// 2009.01.05 版本:4.6 JiRiGaLa MySql 获取前几个的错误进行修正。
/// 2008.06.03 版本:4.5 JiRiGaLa IN SQL语句调试、修改错误。
/// 2008.05.31 版本:4.4 JiRiGaLa 改进 参数名param去掉了,变量名首字母小写了。
/// 2008.05.09 版本:4.3 JiRiGaLa 改进 树型结构的数据的获得函数部分。
/// 2007.11.08 版本:4.2 JiRiGaLa 改进 DataTableToStringList 为 FieldToList。
/// 2007.11.05 版本:4.1 JiRiGaLa 改进 GetDS、GetDT 功能,整体思路又上一个台阶,基类的又一次飞跃。
/// 2007.11.05 版本:4.0 JiRiGaLa 改进 支持不是“ID”为字段的主键表。
/// 2007.11.01 版本:3.9 JiRiGaLa 改进 BUOperatorInfo 去掉这个变量,可以提高性能,提高速度,基类的又一次飞跃。
/// 2007.09.13 版本:3.8 JiRiGaLa 改进 BUBaseBusinessLogic.SQLLogicConditional 错误。
/// 2007.08.14 版本:3.7 JiRiGaLa 改进 WebService 模式下 DataSet 传输数据的速度问题。
/// 2007.07.20 版本:3.6 JiRiGaLa 改进 DataSet 修改为 DataTable 应该能提高一些速度吧。
/// 2007.05.20 版本:3.5 JiRiGaLa 改进 GetList() 方法整理,这次又应该是一次升华,质的飞跃很不容易啊,身心都有提高了。
/// 2007.05.20 版本:3.4 JiRiGaLa 改进 Exists() 方法整理。
/// 2007.05.13 版本:3.3 JiRiGaLa 改进 GetProperty(),SetProperty(),Delete() 方法整理。
/// 2007.05.10 版本:3.2 JiRiGaLa 改进 GetList() 方法整理。
/// 2007.04.10 版本:3.1 JiRiGaLa 添加 GetNextID,GetPreviousID 方法整理。
/// 2007.03.03 版本:3.0 JiRiGaLa 进行了一次彻底的优化工作,方法的位置及功能整理。
/// 2007.03.01 版本:2.0 JiRiGaLa 重新调整代码的规范化。
/// 2006.02.05 版本:1.1 JiRiGaLa 重新调整代码的规范化。
/// 2005.12.30 版本:1.0 JiRiGaLa 数据库连接方式都进行改进
/// 2005.09.04 版本:1.0 JiRiGaLa 执行数据库脚本
/// 2005.08.19 版本:1.0 JiRiGaLa 整理一下编排
/// 2005.07.10 版本:1.0 JiRiGaLa 修改了程序,格式以及理念都有些提高,应该是一次大突破
/// 2004.11.12 版本:1.0 JiRiGaLa 添加了最新的GetParent、GetChildren、GetParentChildren 方法
/// 2004.07.21 版本:1.0 JiRiGaLa UpdateRecord、Delete、SetProperty、GetProperty、ExecuteNonQuery、GetRecord 方法进行改进。
/// 还删除一些重复的代码,提取了最优化的方法,有时候写的代码真的是垃圾,可能自己也没有注意时就写出了垃圾。
/// GetRepeat、GetDayOfWeek、ExecuteProcedure、GetFromProcedure 方法进行改进,基本上把所有的方法都重新写了一遍。
///
/// 版本:4.7
///
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2009.01.15</date>
/// </author>
/// </summary>
public class DbLogic
{
#region public static String SqlSafe(String value) 检查参数的安全性
/// <summary>
/// 检查参数的安全性
/// </summary>
/// <param name="value">参数</param>
/// <returns>安全的参数</returns>
public static String SqlSafe(String value)
{
value = value.Replace("'", "''");
// value = value.Replace("%", "'%");
return value;
}
#endregion
#region public static String GetWhereString(IDbHelper dbHelper, String[] names, ref Object[] values, String relation) 获得条件语句
/// <summary>
/// 获得条件语句
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="relation">逻辑关系</param>
/// <returns>字符串</returns>
public static String GetWhereString(IDbHelper dbHelper, String[] names, ref Object[] values, String relation)
{
String returnValue = String.Empty;
String subSqlQuery = String.Empty;
for (int i = 0; i < names.Length; i++)
{
if ((names[i] != null) && (names[i].Length > 0))
{
//if (values[i] == null || String.IsNullOrEmpty(values[i].ToString()))
if (values[i] == null)
{
subSqlQuery = " (" + names[i] + " IS NULL) ";
}
else
{
// 这里操作,就会有些重复了,不应该进行处理
// values[i] = this.SqlSafe(values[i].ToString());
subSqlQuery = " (" + names[i] + " = " + dbHelper.GetParameter(names[i]) + ") ";
if ((values[i].ToString().IndexOf('[') >= 0) || (values[i].ToString().IndexOf(']') >= 0))
{
values[i] = values[i].ToString().Replace("[", "/[");
values[i] = values[i].ToString().Replace("]", "/]");
values[i] = SqlSafe(values[i].ToString());
subSqlQuery = " (" + names[i] + " LIKE '" + values[i] + "' ESCAPE '/') ";
values[i] = null;
// subSqlQuery = " (" + names[i] + " LIKE ? ESCAPE '/') ";
}
}
returnValue += subSqlQuery + relation;
}
}
if (returnValue.Length > 0)
{
returnValue = returnValue.Substring(0, returnValue.Length - relation.Length - 1);
}
return returnValue;
}
#endregion
//
// 对数据库进行操作的方法
//
#region public static String ExecuteScalar(IDbHelper dbHelper, String sqlQuery) 执行SQL查询
/// <summary>
/// 执行SQL查询
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name=sqlQuery>查询语句</param>
/// <returns>字符串</returns>
public static String ExecuteScalar(IDbHelper dbHelper, String sqlQuery)
{
String returnValue = String.Empty;
Object returnObject = dbHelper.ExecuteScalar(sqlQuery);
if (returnObject != null)
{
returnValue = returnObject.ToString();
}
return returnValue;
}
#endregion
#region public static int ExecuteNonQuery(IDbHelper dbHelper, String sqlQuery) 执行SQL查询
/// <summary>
/// 执行SQL查询
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="sqlQuery">sql查询</param>
/// <returns>影响行数</returns>
public static int ExecuteNonQuery(IDbHelper dbHelper, String sqlQuery)
{
return dbHelper.ExecuteNonQuery(sqlQuery);
}
#endregion
#region public static int ExecuteNonQuery(IDbHelper dbHelper, String[] sqlQuerys) 执行SQL数组查询
/// <summary>
/// 执行SQL数组查询
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="sqlQuerys">查询语句数组</param>
/// <returns>影响行数</returns>
public static int ExecuteNonQuery(IDbHelper dbHelper, String[] sqlQuerys)
{
int returnValue = 0;
String sqlQuery = String.Empty;
for (int i = 0; i < sqlQuerys.Length; i++)
{
sqlQuery = sqlQuerys[i];
// 执行SQL查询
returnValue += ExecuteNonQuery(dbHelper, sqlQuery);
}
return returnValue;
}
#endregion
//
// 读取属性部分
//
#region public static bool IsModifed(IDbHelper dbHelper, String tableName, String id, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">表名</param>
/// <param name="id">代码</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
public static bool IsModifed(IDbHelper dbHelper, String tableName, String id, String oldModifyUserID, String oldModifyDate)
{
return IsModifed(dbHelper, tableName, BaseBusinessLogic.FieldID, id, oldModifyUserID, oldModifyDate);
}
#endregion
#region public static bool IsModifed(IDbHelper dbHelper, String tableName, String fieldName, String fieldValue, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">表名</param>
/// <param name="id">代码</param>
/// <param name="fieldName">字段</param>
/// <param name="fieldValue">值</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
public static bool IsModifed(IDbHelper dbHelper, String tableName, String fieldName, String fieldValue, String oldModifyUserID, String oldModifyDate)
{
bool returnValue = false;
DataTable dataTable = new DataTable(tableName);
String sqlQuery = " SELECT " + BaseBusinessLogic.FieldID
+ "," + BaseBusinessLogic.FieldCreateUserID
+ "," + BaseBusinessLogic.FieldCreateDate
+ "," + BaseBusinessLogic.FieldModifyUserID
+ "," + BaseBusinessLogic.FieldModifyDate
+ " FROM " + tableName
+ " WHERE " + fieldName + " = " + dbHelper.GetParameter(fieldName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(fieldName, fieldValue));
returnValue = IsModifed(dataTable, oldModifyUserID, oldModifyDate);
return returnValue;
}
#endregion
#region private static bool IsModifed(DataTable dataTable, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dataTable">数据表</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
private static bool IsModifed(DataTable dataTable, String oldModifyUserID, String oldModifyDate)
{
bool returnValue = false;
foreach (DataRow dataRow in dataTable.Rows)
{
returnValue = IsModifed(dataRow, oldModifyUserID, oldModifyDate);
}
return returnValue;
}
#endregion
#region public static bool IsModifed(DataRow dataRow, String oldModifyUserID, String oldModifyDate) 数据是否已经被别人修改了
/// <summary>
/// 数据是否已经被别人修改了
/// </summary>
/// <param name="dataRow">数据行</param>
/// <param name="oldModifyUserID">最后修改者</param>
/// <param name="oldModifyDate">最后修改时间</param>
/// <returns>已被修改</returns>
public static bool IsModifed(DataRow dataRow, String oldModifyUserID, String oldModifyDate)
{
bool returnValue = false;
if ((dataRow[BaseBusinessLogic.FieldModifyUserID] == DBNull.Value) || ((dataRow[BaseBusinessLogic.FieldModifyDate] == DBNull.Value)))
{
returnValue = false;
return returnValue;
}
if ((!dataRow[BaseBusinessLogic.FieldModifyUserID].ToString().Equals(oldModifyUserID)) || (!dataRow[BaseBusinessLogic.FieldModifyDate].ToString().Equals(oldModifyDate)))
{
returnValue = true;
return returnValue;
}
return returnValue;
}
#endregion
#region public static String GetProperty(IDbHelper dbHelper, String tableName, String name, String value, String targetField) 读取属性
/// <summary>
/// 读取属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">键值</param>
/// <param name="targetField">获取字段</param>
/// <returns>属性</returns>
public static String GetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String targetField)
{
String returnValue = String.Empty;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
returnValue = GetProperty(dbHelper, tableName, names, values, targetField);
return returnValue;
}
#endregion
#region public static String GetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField) 读取属性
/// <summary>
/// 读取属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="targetField">获取字段</param>
/// <returns>String</returns>
public static String GetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField)
{
String returnValue = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
returnValue = GetProperty(dbHelper, tableName, names, values, targetField);
return returnValue;
}
#endregion
#region public static String GetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField) 读取属性
/// <summary>
/// 读取属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>属性</returns>
public static String GetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField)
{
String returnValue = String.Empty;
String sqlQuery = " SELECT " + targetField
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = returnObject.ToString();
}
return returnValue;
}
#endregion
#region public static String FindID(IDbHelper dbHelper, String tableName, String name, Object value) 获得ID属性
/// <summary>
/// 获得ID属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">键值</param>
/// <returns>属性</returns>
public static String FindID(IDbHelper dbHelper, String tableName, String name, Object value)
{
return GetProperty(dbHelper, tableName, name, value, BaseBusinessLogic.FieldID);
}
#endregion
#region public static String FindID(IDbHelper dbHelper, String tableName, String name1, String value1, String name2, String value2) 获得ID属性
/// <summary>
/// 获得ID属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>String</returns>
public static String FindID(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String returnValue = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
returnValue = GetProperty(dbHelper, tableName, names, values, BaseBusinessLogic.FieldID);
return returnValue;
}
#endregion
#region public static String FindID(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 获得ID属性
/// <summary>
/// 获得ID属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>属性</returns>
public static String FindID(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
return GetProperty(dbHelper, tableName, names, values, BaseBusinessLogic.FieldID);
}
#endregion
//
// 设置属性部分
//
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">条件字段</param>
/// <param name="value">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
// sqlBuilder.SetDBNow(FieldModifyDate);
sqlBuilder.SetWhere(name, value);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">条件字段</param>
/// <param name="value1">条件值</param>
/// <param name="name2">条件字段</param>
/// <param name="value2">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
// sqlBuilder.SetDBNow(FieldModifyDate);
sqlBuilder.SetWhere(name1, value1);
sqlBuilder.SetWhere(name2, value2);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">条件字段</param>
/// <param name="values">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField, Object targetValue)
{
int returnValue = 0;
for (int i = 0; i < values.Length; i++)
{
returnValue += SetProperty(dbHelper, tableName, name, values[i], targetField, targetValue);
}
return returnValue;
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String[] targetFields, Object[] targetValues) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">条件字段</param>
/// <param name="value">条件值</param>
/// <param name="targetFields">更新字段</param>
/// <param name="targetValues">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String name, Object value, String[] targetFields, Object[] targetValues)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
for (int i = 0; i < targetFields.Length; i++)
{
sqlBuilder.SetValue(targetFields[i], targetValues[i]);
}
sqlBuilder.SetDBNow(BaseBusinessLogic.FieldModifyDate);
sqlBuilder.SetWhere(name, value);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int SetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, Object targetValue) 设置属性
/// <summary>
/// 设置属性
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">条件字段</param>
/// <param name="values">条件值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int SetProperty(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, Object targetValue)
{
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
sqlBuilder.SetDBNow(BaseBusinessLogic.FieldModifyDate);
sqlBuilder.SetWhere(names, values);
return sqlBuilder.EndUpdate();
}
#endregion
#region public static int UpdateRecord(IDbHelper dbHelper, String tableName, String name, String value, String targetField, Object targetValue) 更新记录
/// <summary>
/// 更新记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">代码</param>
/// <param name="value">代码值</param>
/// <param name="targetField">更新字段</param>
/// <param name="targetValue">更新值</param>
/// <returns>影响行数</returns>
public static int UpdateRecord(IDbHelper dbHelper, String tableName, String name, String value, String targetField, Object targetValue)
{
int returnValue = 0;
SQLBuilder sqlBuilder = new SQLBuilder(dbHelper);
sqlBuilder.BeginUpdate(tableName);
sqlBuilder.SetValue(targetField, targetValue);
sqlBuilder.SetWhere(name, value);
returnValue = sqlBuilder.EndUpdate();
return returnValue;
}
#endregion
//
// 获取ID部分 获得代码数组 String[] 常用
//
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value) 读取列表部分
/// <summary>
/// 读取列表部分
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values)
{
return GetIDs(dbHelper, tableName, name, values, BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <param name="targetField">目标字段</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object[] values, String targetField)
{
DataTable dataTable = new DataTable(tableName);
String sqlQuery = " SELECT " + targetField
+ " FROM " + tableName
+ " WHERE " + name + " IN (" + BaseBusinessLogic.ObjectsToList(values) + ")";
dbHelper.Fill(dataTable, sqlQuery);
return BaseBusinessLogic.FieldToArray(dataTable, targetField);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField)
{
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String targetField, int topLimit)
{
String[] names = new String[0];
Object[] values = new Object[0];
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="targetField">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField, int topLimit)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
int topLimit = 0;
String order = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="targetField">排序</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField)
{
int topLimit = 0;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="targetField">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String targetField, int topLimit)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <param name="targetField">目标字段</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String name, Object value, String targetField)
{
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
String fillTableName = tableName;
String order = String.Empty;
int topLimit = 0;
return GetIDs(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据集</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField)
{
int topLimit = 0;
return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
}
#endregion
#region public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, int topLimit) 获取数据集
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="targetField">目标字段</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static String[] GetIDs(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String targetField, int topLimit)
{
if (string.IsNullOrEmpty(targetField))
{
targetField = BaseBusinessLogic.FieldID;
}
// 这里是需要完善的功能,完善了这个,是一次重大突破
String sqlQuery = " SELECT " + targetField + " FROM " + tableName;
String whereSql = String.Empty;
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery = " SELECT TOP " + topLimit.ToString() + targetField + " FROM " + tableName;
break;
case DataBaseType.Oracle:
whereSql = " ROWNUM < = " + topLimit;
break;
}
}
String subSql = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
if (subSql.Length > 0)
{
if (whereSql.Length > 0)
{
whereSql = whereSql + BaseBusinessLogic.SQLLogicConditional + subSql;
}
else
{
whereSql = subSql;
}
}
if (whereSql.Length > 0)
{
sqlQuery += " WHERE " + whereSql;
}
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.MySql:
sqlQuery += " LIMIT 0, " + topLimit;
break;
}
}
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return BaseBusinessLogic.FieldToArray(dataTable, targetField);
}
#endregion
//
// 读取列表部分 填充DataTable 常用
//
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value) 读取列表部分
/// <summary>
/// 读取列表部分
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values)
{
return GetDT(dbHelper, tableName, name, values, String.Empty);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName)
{
String order = String.Empty;
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order)
{
int topLimit = 0;
String[] names = new String[0];
Object[] values = new Object[0];
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="ids">代码数组</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids)
{
return GetDT(dbHelper, tableName, BaseBusinessLogic.FieldID, ids);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="ids">代码数组</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, Object[] ids, String order)
{
return GetDT(dbHelper, tableName, BaseBusinessLogic.FieldID, ids, order);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String order, int topLimit)
{
String[] names = new String[0];
Object[] values = new Object[0];
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order, int topLimit)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
int topLimit = 0;
String order = String.Empty;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order)
{
int topLimit = 0;
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order, int topLimit) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String order, int topLimit)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object value, String order)
{
int topLimit = 0;
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String name, Object[] values, String order)
{
DataTable dataTable = new DataTable(tableName);
String sqlQuery = " SELECT * "
+ " FROM " + tableName
+ " WHERE " + name + " IN (" + BaseBusinessLogic.ObjectsToList(values) + ")";
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
dbHelper.Fill(dataTable, sqlQuery);
return dataTable;
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
String fillTableName = tableName;
String order = String.Empty;
int topLimit = 0;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order) 获取数据表
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">填充目标表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="order">排序</param>
/// <returns>数据集</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order)
{
int topLimit = 0;
return GetDT(dbHelper, tableName, names, values, order, topLimit);
}
#endregion
#region public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order, int topLimit) 获取数据集
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">数据来源表名</param>
/// <param name="names">字段名</param>
/// <param name="values">字段值</param>
/// <param name="order">排序</param>
/// <param name="topLimit">前几个记录</param>
/// <returns>数据表</returns>
public static DataTable GetDT(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String order, int topLimit)
{
// 这里是需要完善的功能,完善了这个,是一次重大突破
String sqlQuery = " SELECT * FROM " + tableName;
String whereSql = String.Empty;
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery = " SELECT TOP " + topLimit.ToString() + " * FROM " + tableName;
break;
case DataBaseType.Oracle:
whereSql = " ROWNUM < = " + topLimit;
break;
}
}
String subSql = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
if (subSql.Length > 0)
{
if (whereSql.Length > 0)
{
whereSql = whereSql + BaseBusinessLogic.SQLLogicConditional + subSql;
}
else
{
whereSql = subSql;
}
}
if (whereSql.Length > 0)
{
sqlQuery += " WHERE " + whereSql;
}
if ((order != null) && (order.Length > 0))
{
sqlQuery += " ORDER BY " + order;
}
if (topLimit != 0)
{
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.MySql:
sqlQuery += " LIMIT 0, " + topLimit;
break;
}
}
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
//
// 读取列表部分 不常用
//
#region public static DataTable GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName) 通过存储过程获取表数据
/// <summary>
/// 通过存储过程获取表数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="procedureName">存储过程名</param>
/// <param name="tableName">填充表</param>
/// <returns>数据集</returns>
public static DataTable GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName)
{
return dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, null);
}
#endregion
#region public static DataSet GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName, String id) 通过存储过程获取表数据
/// <summary>
/// 通过存储过程获取表数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="procedureName">存储过程名</param>
/// <param name="tableName">填充表</param>
/// <param name="id">代码值</param>
/// <returns>数据集</returns>
public static DataTable GetFromProcedure(IDbHelper dbHelper, String procedureName, String tableName, String id)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = BaseBusinessLogic.FieldID;
values[0] = id;
return dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, dbHelper.MakeParameters(names, values));
}
#endregion
//
// 记录是否存在
//
#region public static bool Exists(IDbHelper dbHelper, String tableName, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="id">字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String id)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = BaseBusinessLogic.FieldID;
values[0] = id;
return Exists(dbHelper, tableName, names, values);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return Exists(dbHelper, tableName, names, values);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String[] ids) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="ids">字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String[] ids)
{
bool returnValue = false;
for (int i = 0; i < ids.Length; i++)
{
returnValue = Exists(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
if (returnValue)
{
break;
}
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name, String[] values) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">字段名</param>
/// <param name="values">字段值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name, String[] values)
{
bool returnValue = false;
for (int i = 0; i < values.Length; i++)
{
returnValue = Exists(dbHelper, tableName, name, values[i]);
if (returnValue)
{
break;
}
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name, Object value, String id)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) FROM " + tableName;
if (name == null)
{
sqlQuery += " WHERE (" + name + " IS NULL ) ";
}
else
{
sqlQuery += " WHERE (" + name + " = " + dbHelper.GetParameter(name) + ") ";
}
if (id.Length > 0)
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + BaseBusinessLogic.FieldID + " <> " + dbHelper.GetParameter(BaseBusinessLogic.FieldID) + " ) ";
}
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name;
values[0] = value;
if (id.Length > 0)
{
names[1] = BaseBusinessLogic.FieldID;
values[1] = id;
}
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
return Exists(dbHelper, tableName, names, values);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String name, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="name">目标字段名</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String id)
{
return Exists(dbHelper, tableName, name1, value1, name2, value2, BaseBusinessLogic.FieldID, id);
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String name, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <param name="name">目标字段名</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2, String name, String id)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) FROM " + tableName;
if (value1 == null)
{
sqlQuery += " WHERE (" + name1 + " IS NULL) ";
}
else
{
sqlQuery += " WHERE (" + name1 + " = " + dbHelper.GetParameter(name1) + ") ";
}
if (value2 == null)
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "(" + name2 + " IS NULL) ";
}
else
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "(" + name2 + " = " + dbHelper.GetParameter(name2) + ") ";
}
if (id.Length > 0)
{
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + name + " <> " + dbHelper.GetParameter(name) + ") ";
}
String[] names = new String[3];
Object[] values = new Object[3];
names[0] = name1;
values[0] = value1;
names[1] = name2;
values[1] = value2;
if (id.Length > 0)
{
names[2] = name;
values[2] = id;
}
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) "
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
#region public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String name, String id) 记录是否存在
/// <summary>
/// 记录是否存在
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <param name="name">目标字段名</param>
/// <param name="id">代码值</param>
/// <returns>bool</returns>
public static bool Exists(IDbHelper dbHelper, String tableName, String[] names, Object[] values, String name, String id)
{
bool returnValue = false;
String sqlQuery = " SELECT COUNT(*) "
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
sqlQuery += BaseBusinessLogic.SQLLogicConditional + "( " + BaseBusinessLogic.FieldID + " <> '" + id + "' ) ";
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
returnValue = int.Parse(returnObject.ToString()) > 0;
}
return returnValue;
}
#endregion
//
// 删除数据库方法
//
#region public static int Delete(IDbHelper dbHelper, String tableName) 删除表中的所有数据
/// <summary>
/// 删除表中的所有数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName)
{
return BatchDelete(dbHelper, tableName, new String[0], new Object[0]);
}
#endregion
#region public static int Delete(IDbHelper dbHelper, String tableName, String id) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <param name="id">字段值</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName, String id)
{
return Delete(dbHelper, tableName, BaseBusinessLogic.FieldID, id);
}
#endregion
#region public static int BatchDelete(IDbHelper dbHelper, String tableName, String[] ids) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <param name="ids">字段值</param>
/// <returns>影响行数</returns>
public static int BatchDelete(IDbHelper dbHelper, String tableName, String[] ids)
{
int returnValue = 0;
for (int i = 0; i < ids.Length; i++)
{
returnValue += Delete(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
}
return returnValue;
}
#endregion
#region public static int BatchDelete(IDbHelper dbHelper, String tableName, String name, Object[] values) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表</param>
/// <param name="name">目标字段名</param>
/// <param name="values">目标字段值</param>
/// <returns>影响行数</returns>
public static int BatchDelete(IDbHelper dbHelper, String tableName, String name, Object[] values)
{
int returnValue = 0;
for (int i = 0; i < values.Length; i++)
{
returnValue += Delete(dbHelper, tableName, name, values[i]);
}
return returnValue;
}
#endregion
#region public static int Delete(IDbHelper dbHelper, String tableName, String name, Object value) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表格名</param>
/// <param name="name">代码</param>
/// <param name="value">代码值</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName, String name, Object value)
{
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = name;
values[0] = value;
return BatchDelete(dbHelper, tableName, names, values);
}
#endregion
#region public static int Delete(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>影响行数</returns>
public static int Delete(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
return BatchDelete(dbHelper, tableName, names, values);
}
#endregion
#region public int static BatchDelete(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 删除表格数据
/// <summary>
/// 删除表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <returns>影响行数</returns>
public static int BatchDelete(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
String sqlQuery = " DELETE " + " FROM " + tableName;
String whereString = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
if (whereString.Length > 0)
{
sqlQuery += " WHERE " + whereString;
}
return dbHelper.ExecuteNonQuery(sqlQuery, dbHelper.MakeParameters(names, values));
}
#endregion
#region public static int Truncate(IDbHelper dbHelper, String tableName) 截断表格数据
/// <summary>
/// 截断表格数据
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表格</param>
/// <returns>是否成功</returns>
public static int Truncate(IDbHelper dbHelper, String tableName)
{
String sqlQuery = " Truncate Table " + tableName;
return dbHelper.ExecuteNonQuery(sqlQuery);
}
#endregion
//
// 锁定表记录
//
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String id) 锁定表记录
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="id">字段值</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String id)
{
return LockNoWait(dbHelper, tableName, BaseBusinessLogic.FieldID, id);
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] ids) 锁定表记录
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="ids">字段值</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] ids)
{
int returnValue = 0;
for (int i = 0; i < ids.Length; i++)
{
returnValue += LockNoWait(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
}
return returnValue;
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String name, Object value) 锁定表记录
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name">目标字段名</param>
/// <param name="value">目标字段值</param>
/// <param name="id">代码值</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String name, Object value)
{
int returnValue = 0;
String sqlQuery = " SELECT " + BaseBusinessLogic.FieldID + " FROM " + tableName;
if (name == null)
{
sqlQuery += " WHERE (" + name + " IS NULL ) ";
}
else
{
sqlQuery += " WHERE (" + name + " = " + dbHelper.GetParameter(name) + ")";
}
sqlQuery += " FOR UPDATE NOWAIT ";
try
{
DataSet dataSet = new DataSet();
dbHelper.Fill(dataSet, sqlQuery, "ForUpdateNoWait", dbHelper.MakeParameters(name, value));
returnValue = dataSet.Tables["ForUpdateNoWait"].Rows.Count;
}
catch
{
returnValue = -1;
}
return returnValue;
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2) 记录是否存在
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="name1">目标字段名1</param>
/// <param name="value1">目标字段值1</param>
/// <param name="name2">目标字段名2</param>
/// <param name="value2">目标字段值2</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String name1, Object value1, String name2, Object value2)
{
String[] names = new String[2];
Object[] values = new Object[2];
names[0] = name1;
names[1] = name2;
values[0] = value1;
values[1] = value2;
return LockNoWait(dbHelper, tableName, names, values);
}
#endregion
#region public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] names, Object[] values) 记录是否存在
/// <summary>
/// 锁定表记录
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表名</param>
/// <param name="names">字段名数组</param>
/// <param name="values">键值数组</param>
/// <param name="targetField">获取字段</param>
/// <returns>锁定的行数</returns>
public static int LockNoWait(IDbHelper dbHelper, String tableName, String[] names, Object[] values)
{
int returnValue = 0;
String sqlQuery = " SELECT " + BaseBusinessLogic.FieldID
+ " FROM " + tableName
+ " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
sqlQuery += " FOR UPDATE NOWAIT ";
try
{
DataSet dataSet = new DataSet();
dbHelper.Fill(dataSet, sqlQuery, "ForUpdateNoWait", dbHelper.MakeParameters(names, values));
returnValue = dataSet.Tables["ForUpdateNoWait"].Rows.Count;
}
catch
{
returnValue = -1;
}
return returnValue;
}
#endregion
//
// 树型结构的算法
//
#region public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父节点列表
/// <summary>
/// 获取父节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + BaseBusinessLogic.FieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName;
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery += " WHERE (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") ";
break;
case DataBaseType.Oracle:
sqlQuery += " WHERE (SUBSTR(" + dbHelper.GetParameter(fieldCode) + ", 1, LENGTH(" + fieldCode + ")) = " + fieldCode + ") ";
break;
}
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = fieldCode;
values[0] = code;
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order, bool idOnly) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="id">值</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + fieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName
+ " START WITH " + fieldID + " = " + dbHelper.GetParameter(fieldID)
+ " CONNECT BY PRIOR " + fieldID + " = " + fieldParentID;
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[1];
names[0] = fieldID;
Object[] values = new Object[1];
values[0] = id;
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String[] ids, String fieldParentID, String order, bool idOnly) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="ids">代码数组</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String[] ids, String fieldParentID, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + fieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName
+ " START WITH " + fieldID + " IN (" + BaseBusinessLogic.ObjectsToList(ids) + ")"
+ " CONNECT BY PRIOR " + fieldID + " = " + fieldParentID;
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery);
return dataTable;
}
#endregion
#region private static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
private static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + BaseBusinessLogic.FieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName;
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery += " WHERE (LEFT(" + fieldCode + ", LEN(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + " ) ";
break;
case DataBaseType.Oracle:
sqlQuery += " WHERE (SUBSTR(" + fieldCode + ", 1, LENGTH(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + " ) ";
break;
}
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[2];
names[0] = fieldCode;
names[1] = fieldCode;
Object[] values = new Object[2];
values[0] = code;
values[1] = code;
DataTable dataTable = new DataTable(tableName);
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region private static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly) 获取父子节点列表
/// <summary>
/// 获取父子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <param name="idOnly">只需要代码</param>
/// <returns>数据表</returns>
private static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order, bool idOnly)
{
String sqlQuery = String.Empty;
if (idOnly)
{
sqlQuery = " SELECT " + BaseBusinessLogic.FieldID;
}
else
{
sqlQuery = " SELECT * ";
}
sqlQuery += " FROM " + tableName;
switch (BaseConfiguration.Instance.DataBaseType)
{
case DataBaseType.Access:
case DataBaseType.Sqlserver:
sqlQuery += " WHERE (LEFT(" + fieldCode + ", LEN(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + ") ";
sqlQuery += " OR (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") ";
break;
case DataBaseType.Oracle:
sqlQuery += " WHERE (SUBSTR(" + fieldCode + ", 1, LENGTH(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + ") ";
sqlQuery += " OR (" + fieldCode + " = SUBSTR(" + dbHelper.GetParameter(fieldCode) + ", 1, LENGTH(" + fieldCode + "))) ";
break;
}
if (!String.IsNullOrEmpty(order))
{
sqlQuery += " ORDER BY " + order;
}
String[] names = new String[3];
names[0] = fieldCode;
names[1] = fieldCode;
names[2] = fieldCode;
Object[] values = new Object[3];
values[0] = code;
values[1] = code;
values[2] = code;
DataTable dataTable = new DataTable();
dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
return dataTable;
}
#endregion
#region public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父节点列表
/// <summary>
/// 获取父节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetParentsByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return GetParentsByCode(dbHelper, tableName, fieldCode, code, order, false);
}
#endregion
#region public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="id">值</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetChildrens(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order)
{
return GetChildrens(dbHelper, tableName, fieldID, id, fieldParentID, order, false);
}
#endregion
#region public static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return GetChildrensByCode(dbHelper, tableName, fieldCode, code, order, false);
}
#endregion
#region public static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父子节点列表
/// <summary>
/// 获取父子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>数据表</returns>
public static DataTable GetParentChildrensByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return GetParentChildrensByCode(dbHelper, tableName, fieldCode, code, order, false);
}
#endregion
#region public static String[] GetParentsIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父节点列表
/// <summary>
/// 获取父节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetParentsIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return BaseBusinessLogic.FieldToArray(GetParentsByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetChildrensID(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldID">主键字段</param>
/// <param name="id">值</param>
/// <param name="fieldParentID">父亲节点字段</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetChildrensID(IDbHelper dbHelper, String tableName, String fieldID, String id, String fieldParentID, String order)
{
return BaseBusinessLogic.FieldToArray(GetChildrens(dbHelper, tableName, fieldID, id, fieldParentID, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetChildrensByIDCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取子节点列表
/// <summary>
/// 获取子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetChildrensIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return BaseBusinessLogic.FieldToArray(GetChildrensByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String[] GetParentChildrensIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order) 获取父子节点列表
/// <summary>
/// 获取父子节点列表
/// </summary>
/// <param name="dbHelper">数据库连接</param>
/// <param name="tableName">目标表明</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编码</param>
/// <param name="order">排序</param>
/// <returns>代码数组</returns>
public static String[] GetParentChildrensIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code, String order)
{
return BaseBusinessLogic.FieldToArray(GetParentChildrensByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
}
#endregion
#region public static String GetParentIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code) 获取父节点
/// <summary>
/// 获取父节点
/// </summary>
/// <param name="tableName">目标表名</param>
/// <param name="fieldCode">编码字段</param>
/// <param name="code">编号</param>
/// <returns>代码</returns>
public static String GetParentIDByCode(IDbHelper dbHelper, String tableName, String fieldCode, String code)
{
String parentID = String.Empty;
String sqlQuery = " SELECT MAX(ID) AS ID "
+ " FROM " + tableName
+ " WHERE (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") "
+ " AND " + fieldCode + " <> '" + code + " ' ";
String[] names = new String[1];
Object[] values = new Object[1];
names[0] = fieldCode;
values[0] = code;
Object returnObject = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
if (returnObject != null)
{
parentID = returnObject.ToString();
}
return parentID;
}
#endregion
}
}
代码生成器相关连接请看:
C# Code Builder V1.2 (C#代码生成器) 产生支持多种数据库的实体代码
针对C#.NET各种代码生成器的一点小建议
C#版代码生成器 - 支持PowerDesigner设计文档
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。