using NFine.Data.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NFine.Repository
{
public class Database : IDatabase, IDisposable
{
#region 构造函数
public static string connString { get; set; }
///
/// 构造方法
///
public Database(string connstring)
{
DbHelper dbhelper = new DbHelper(connstring);
}
///
/// 数据库连接对象
///
private DbConnection dbConnection { get; set; }
///
/// 事务对象
///
private DbTransaction isOpenTrans { get; set; }
///
/// 是否已在事务之中
///
public bool inTransaction { get; set; }
///
/// 事务开始
///
///
public DbTransaction BeginTrans()
{
if (!this.inTransaction)
{
dbConnection = DbFactory.CreateDbConnection(DbHelper.ConnectionString);
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
inTransaction = true;
isOpenTrans = dbConnection.BeginTransaction();
}
return isOpenTrans;
}
///
/// 提交事务
///
public void Commit()
{
if (this.inTransaction)
{
this.inTransaction = false;
this.isOpenTrans.Commit();
this.Close();
}
}
///
/// 回滚事务
///
public void Rollback()
{
if (this.inTransaction)
{
this.inTransaction = false;
this.isOpenTrans.Rollback();
this.Close();
}
}
///
/// 关闭数据库连接
///
public void Close()
{
if (this.dbConnection != null)
{
this.dbConnection.Close();
this.dbConnection.Dispose();
}
if (this.isOpenTrans != null)
{
this.isOpenTrans.Dispose();
}
this.dbConnection = null;
this.isOpenTrans = null;
}
///
/// 内存回收
///
public void Dispose()
{
if (this.dbConnection != null)
{
this.dbConnection.Dispose();
}
if (this.isOpenTrans != null)
{
this.isOpenTrans.Dispose();
}
}
#endregion
#region SqlBulkCopy大批量数据插入
///
/// 大批量数据插入
///
/// 资料表
///
public bool BulkInsert(DataTable datatable)
{
return false;
}
#endregion
#region 执行SQL语句
///
/// 执行SQL语句
///
/// Sql语句
///
public int ExecuteBySql(StringBuilder strSql)
{
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString());
}
///
/// 执行SQL语句
///
/// Sql语句
/// 事务对象
///
public int ExecuteBySql(StringBuilder strSql, DbTransaction isOpenTrans)
{
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString());
}
///
/// 执行SQL语句
///
/// Sql语句
/// sql语句对应参数
///
public int ExecuteBySql(StringBuilder strSql, DbParameter[] parameters)
{
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
}
///
/// 执行SQL语句
///
/// Sql语句
/// sql语句对应参数
/// 事务对象
///
public int ExecuteBySql(StringBuilder strSql, DbParameter[] parameters, DbTransaction isOpenTrans)
{
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameters);
}
#endregion
#region 执行存储过程
///
/// 执行存储过程
///
/// 存储过程
///
public int ExecuteByProc(string procName)
{
return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, procName);
}
///
/// 执行存储过程
///
/// 存储过程
/// 事务对象
///
public int ExecuteByProc(string procName, DbTransaction isOpenTrans)
{
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.StoredProcedure, procName);
}
///
/// 执行存储过程
///
/// 存储过程
/// sql语句对应参数
///
public int ExecuteByProc(string procName, DbParameter[] parameters)
{
return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, procName, parameters);
}
///
/// 执行存储过程
///
/// 存储过程
/// sql语句对应参数
/// 事务对象
///
public int ExecuteByProc(string procName, DbParameter[] parameters, DbTransaction isOpenTrans)
{
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.StoredProcedure, procName, parameters);
}
#endregion
#region 插入数据
///
/// 插入数据
///
/// 实体类对象
///
public int Insert(T entity)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.InsertSql(entity);
DbParameter[] parameter = DatabaseCommon.GetParameter(entity);
val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
///
/// 插入数据
///
/// 实体类对象
/// 事务对象
///
public int Insert(T entity, DbTransaction isOpenTrans)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.InsertSql(entity);
DbParameter[] parameter = DatabaseCommon.GetParameter(entity);
val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
///
/// 批量插入数据
///
/// 实体类对象
///
public int Insert(List entity)
{
object val = 0;
DbTransaction isOpenTrans = this.BeginTrans();
try
{
foreach (var item in entity)
{
this.Insert(item, isOpenTrans);
}
this.Commit();
val = 1;
}
catch (Exception ex)
{
this.Rollback();
this.Close();
val = -1;
throw ex;
}
return Convert.ToInt32(val);
}
///
/// 批量插入数据
///
/// 实体类对象
/// 事务对象
///
public int Insert(List entity, DbTransaction isOpenTrans)
{
object val = 0;
try
{
foreach (var item in entity)
{
this.Insert(item, isOpenTrans);
}
val = 1;
}
catch (Exception ex)
{
val = -1;
throw ex;
}
return Convert.ToInt32(val);
}
///
/// 插入数据
///
/// 表名
/// 哈希表键值
///
public int Insert(string tableName, Hashtable ht)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.InsertSql(tableName, ht);
DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
///
/// 插入数据
///
/// 表名
/// 哈希表键值
/// 事务对象
///
public int Insert(string tableName, Hashtable ht, DbTransaction isOpenTrans)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.InsertSql(tableName, ht);
DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
#endregion
#region 修改数据
///
/// 修改数据
///
/// 实体对象
///
public int Update(T entity)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.UpdateSql(entity);
DbParameter[] parameter = DatabaseCommon.GetParameter(entity);
val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
///
/// 修改数据
///
/// 实体对象
/// 事务对象
///
public int Update(T entity, DbTransaction isOpenTrans)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.UpdateSql(entity);
DbParameter[] parameter = DatabaseCommon.GetParameter(entity);
val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
///
/// 修改数据
///
/// 实体属性名称
/// 字段值
///
public int Update(string propertyName, string propertyValue)
{
object val = 0;
StringBuilder strSql = new StringBuilder();
StringBuilder sb = new StringBuilder();
sb.Append("Update ");
sb.Append(typeof(T).Name);
sb.Append(" Set ");
sb.Append(propertyName);
sb.Append("=");
sb.Append(DbHelper.DbParmChar + propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
return Convert.ToInt32(val);
}
///
/// 修改数据
///
/// 实体属性名称
/// 字段值
/// 事务对象
///
public int Update(string propertyName, string propertyValue, DbTransaction isOpenTrans)
{
object val = 0;
StringBuilder strSql = new StringBuilder();
StringBuilder sb = new StringBuilder();
sb.Append("Update ");
sb.Append(typeof(T).Name);
sb.Append(" Set ");
sb.Append(propertyName);
sb.Append("=");
sb.Append(DbHelper.DbParmChar + propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
return Convert.ToInt32(val);
}
///
/// 批量修改数据
///
/// 实体对象
///
public int Update(List entity)
{
object val = 0;
DbTransaction isOpenTrans = this.BeginTrans();
try
{
foreach (var item in entity)
{
this.Update(item, isOpenTrans);
}
this.Commit();
val = 1;
}
catch (Exception ex)
{
this.Rollback();
this.Close();
val = -1;
throw ex;
}
return Convert.ToInt32(val);
}
///
/// 批量修改数据
///
/// 实体对象
/// 事务对象
///
public int Update(List entity, DbTransaction isOpenTrans)
{
object val = 0;
try
{
foreach (var item in entity)
{
this.Update(item, isOpenTrans);
}
val = 1;
}
catch (Exception ex)
{
val = -1;
throw ex;
}
return Convert.ToInt32(val);
}
///
/// 修改数据
///
/// 表名
/// 哈希表键值
/// 主键字段
///
public int Update(string tableName, Hashtable ht, string propertyName)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.UpdateSql(tableName, ht, propertyName);
DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
///
/// 修改数据
///
/// 表名
/// 哈希表键值
/// 主键字段
/// 事务对象
///
public int Update(string tableName, Hashtable ht, string propertyName, DbTransaction isOpenTrans)
{
object val = 0;
StringBuilder strSql = DatabaseCommon.UpdateSql(tableName, ht, propertyName);
DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
return Convert.ToInt32(val);
}
#endregion
#region 删除数据
///
/// 删除数据
///
/// 实体类
///
public int Delete(T entity)
{
StringBuilder strSql = DatabaseCommon.DeleteSql(entity);
DbParameter[] parameter = DatabaseCommon.GetParameter(entity);
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
}
///
/// 删除数据
///
/// 实体类
/// 事务对象
///
public int Delete(T entity, DbTransaction isOpenTrans)
{
StringBuilder strSql = DatabaseCommon.DeleteSql(entity);
DbParameter[] parameter = DatabaseCommon.GetParameter(entity);
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 主键值
///
public int Delete(object propertyValue)
{
string tableName = typeof(T).Name;//获取表名
string pkName = DatabaseCommon.GetKeyField().ToString();//获取主键
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, pkName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 主键值
/// 事务对象
///
public int Delete(object propertyValue, DbTransaction isOpenTrans)
{
string tableName = typeof(T).Name;//获取表名
string pkName = DatabaseCommon.GetKeyField().ToString();//获取主键
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, pkName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 实体属性名称
/// 字段值
///
public int Delete(string propertyName, string propertyValue)
{
string tableName = typeof(T).Name;//获取表名
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 实体属性名称
/// 字段值
/// 事务对象
///
public int Delete(string propertyName, string propertyValue, DbTransaction isOpenTrans)
{
string tableName = typeof(T).Name;//获取表名
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 表名
/// 实体属性名称
/// 字段值
///
public int Delete(string tableName, string propertyName, string propertyValue)
{
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 表名
/// 实体属性名称
/// 字段值
/// 事务对象
///
public int Delete(string tableName, string propertyName, string propertyValue, DbTransaction isOpenTrans)
{
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
}
///
/// 删除数据
///
/// 表名
/// 键值生成SQL条件
///
public int Delete(string tableName, Hashtable ht)
{
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, ht);
DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
}
///
/// 删除数据
///
/// 表名
/// 键值生成SQL条件
/// 事务对象
///
public int Delete(string tableName, Hashtable ht, DbTransaction isOpenTrans)
{
StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, ht);
DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
}
///
/// 批量删除数据
///
/// 主键值:数组1,2,3,4,5,6.....
///
public int Delete(object[] propertyValue)
{
string tableName = typeof(T).Name;//获取表名
string pkName = DatabaseCommon.GetKeyField().ToString();//获取主键
StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + pkName + " IN (");
try
{
IList parameter = new List();
int index = 0;
string str = DbHelper.DbParmChar + "ID" + index;
for (int i = 0; i < (propertyValue.Length - 1); i++)
{
object obj2 = propertyValue[i];
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str).Append(",");
parameter.Add(DbFactory.CreateDbParameter(str, obj2));
index++;
}
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str);
parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
strSql.Append(")");
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 批量删除数据
///
/// 主键值:数组1,2,3,4,5,6.....
/// 事务对象
///
public int Delete(object[] propertyValue, DbTransaction isOpenTrans)
{
string tableName = typeof(T).Name;//获取表名
string pkName = DatabaseCommon.GetKeyField().ToString();//获取主键
StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
try
{
IList parameter = new List();
int index = 0;
string str = DbHelper.DbParmChar + "ID" + index;
for (int i = 0; i < (propertyValue.Length - 1); i++)
{
object obj2 = propertyValue[i];
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str).Append(",");
parameter.Add(DbFactory.CreateDbParameter(str, obj2));
index++;
}
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str);
parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
strSql.Append(")");
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 批量删除数据
///
/// 实体属性名称
/// 字段值:数组1,2,3,4,5,6.....
///
public int Delete(string propertyName, object[] propertyValue)
{
string tableName = typeof(T).Name;//获取表名
string pkName = propertyName;
StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
try
{
IList parameter = new List();
int index = 0;
string str = DbHelper.DbParmChar + "ID" + index;
for (int i = 0; i < (propertyValue.Length - 1); i++)
{
object obj2 = propertyValue[i];
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str).Append(",");
parameter.Add(DbFactory.CreateDbParameter(str, obj2));
index++;
}
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str);
parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
strSql.Append(")");
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 批量删除数据
///
/// 实体属性名称
/// 字段值:数组1,2,3,4,5,6.....
/// 事务对象
///
public int Delete(string propertyName, object[] propertyValue, DbTransaction isOpenTrans)
{
string tableName = typeof(T).Name;//获取表名
string pkName = propertyName;
StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
try
{
IList parameter = new List();
int index = 0;
string str = DbHelper.DbParmChar + "ID" + index;
for (int i = 0; i < (propertyValue.Length - 1); i++)
{
object obj2 = propertyValue[i];
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str).Append(",");
parameter.Add(DbFactory.CreateDbParameter(str, obj2));
index++;
}
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str);
parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
strSql.Append(")");
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 批量删除数据
///
/// 表名
/// 实体属性名称
/// 字段值:数组1,2,3,4,5,6.....
///
public int Delete(string tableName, string propertyName, object[] propertyValue)
{
string pkName = propertyName;
StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
try
{
IList parameter = new List();
int index = 0;
string str = DbHelper.DbParmChar + "ID" + index;
for (int i = 0; i < (propertyValue.Length - 1); i++)
{
object obj2 = propertyValue[i];
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str).Append(",");
parameter.Add(DbFactory.CreateDbParameter(str, obj2));
index++;
}
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str);
parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
strSql.Append(")");
return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 批量删除数据
///
/// 表名
/// 实体属性名称
/// 字段值:数组1,2,3,4,5,6.....
/// 事务对象
///
public int Delete(string tableName, string propertyName, object[] propertyValue, DbTransaction isOpenTrans)
{
string pkName = propertyName;
StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
try
{
IList parameter = new List();
int index = 0;
string str = DbHelper.DbParmChar + "ID" + index;
for (int i = 0; i < (propertyValue.Length - 1); i++)
{
object obj2 = propertyValue[i];
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str).Append(",");
parameter.Add(DbFactory.CreateDbParameter(str, obj2));
index++;
}
str = DbHelper.DbParmChar + "ID" + index;
strSql.Append(str);
parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
strSql.Append(")");
return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 查询数据列表、返回List
///
/// 查询数据列表、返回List
///
/// 显示条数
///
public List FindListTop(int Top) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 显示条数
/// 实体属性名称
/// 字段值
///
public List FindListTop(int Top, string propertyName, string propertyValue) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
strSql.Append(" AND " + propertyName + " = " + DbHelper.DbParmChar + propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 显示条数
/// 条件
///
public List FindListTop(int Top, string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 显示条数
/// 条件
/// sql语句对应参数
///
public List FindListTop(int Top, string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
///
public List FindList() where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 实体属性名称
/// 字段值
///
public List FindList(string propertyName, string propertyValue) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(" AND " + propertyName + " = " + DbHelper.DbParmChar + propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 条件
///
public List FindList(string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 条件
/// sql语句对应参数
///
public List FindList(string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// Sql语句
///
public List FindListBySql(string strSql)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// Sql语句
/// sql语句对应参数
///
public List FindListBySql(string strSql, DbParameter[] parameters)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToList(dr);
}
///
/// 查询数据列表、返回List
///
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public List FindListPage(string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
return SqlServerHelper.GetPageList(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回List
///
/// 条件
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public List FindListPage(string WhereSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
return SqlServerHelper.GetPageList(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回List
///
/// 条件
/// sql语句对应参数
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public List FindListPage(string WhereSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
return SqlServerHelper.GetPageList(strSql.ToString(), parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回List
///
/// Sql语句
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public List FindListPageBySql(string strSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
{
return SqlServerHelper.GetPageList(strSql, orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回List
///
/// Sql语句
/// sql语句对应参数
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public List FindListPageBySql(string strSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
{
return SqlServerHelper.GetPageList(strSql, parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
}
#endregion
#region 查询数据列表、返回DataTable
///
/// 查询数据列表、返回 DataTable
///
/// 显示条数
///
public DataTable FindTableTop(int Top) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// 显示条数
/// 条件
///
public DataTable FindTableTop(int Top, string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// 显示条数
/// 条件
/// sql语句对应参数
///
public DataTable FindTableTop(int Top, string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(Top);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
///
public DataTable FindTable() where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// 条件
///
public DataTable FindTable(string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// 条件
/// sql语句对应参数
///
public DataTable FindTable(string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// Sql语句
///
public DataTable FindTableBySql(string strSql)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// Sql语句
/// sql语句对应参数
///
public DataTable FindTableBySql(string strSql, DbParameter[] parameters)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public DataTable FindTablePage(string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
return SqlServerHelper.GetPageTable(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回 DataTable
///
/// 条件
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public DataTable FindTablePage(string WhereSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
return SqlServerHelper.GetPageTable(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回 DataTable
///
/// 条件
/// sql语句对应参数
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public DataTable FindTablePage(string WhereSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql();
strSql.Append(WhereSql);
return SqlServerHelper.GetPageTable(strSql.ToString(), parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回 DataTable
///
/// Sql语句
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public DataTable FindTablePageBySql(string strSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
{
return SqlServerHelper.GetPageTable(strSql, orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回 DataTable
///
/// Sql语句
/// sql语句对应参数
/// 排序字段
/// 排序类型
/// 当前页
/// 页大小
/// 返回查询条数
///
public DataTable FindTablePageBySql(string strSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
{
return SqlServerHelper.GetPageTable(strSql, parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
}
///
/// 查询数据列表、返回 DataTable
///
/// 存储过程
///
public DataTable FindTableByProc(string procName)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, procName);
return DatabaseReader.ReaderToDataTable(dr);
}
///
/// 查询数据列表、返回 DataTable
///
/// 存储过程
/// sql语句对应参数
///
public DataTable FindTableByProc(string procName, DbParameter[] parameters)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, procName, parameters);
return DatabaseReader.ReaderToDataTable(dr);
}
#endregion
#region 查询数据列表、返回DataSet
///
/// 查询数据列表、返回DataSet
///
/// Sql语句
///
public DataSet FindDataSetBySql(string strSql)
{
return DbHelper.GetDataSet(CommandType.Text, strSql);
}
///
/// 查询数据列表、返回DataSet
///
/// Sql语句
/// sql语句对应参数
///
public DataSet FindDataSetBySql(string strSql, DbParameter[] parameters)
{
return DbHelper.GetDataSet(CommandType.Text, strSql, parameters);
}
///
/// 查询数据列表、返回DataSet
///
/// 存储过程
///
public DataSet FindDataSetByProc(string procName)
{
return DbHelper.GetDataSet(CommandType.StoredProcedure, procName);
}
///
/// 查询数据列表、返回DataSet
///
/// 存储过程
/// sql语句对应参数
///
public DataSet FindDataSetByProc(string procName, DbParameter[] parameters)
{
return DbHelper.GetDataSet(CommandType.StoredProcedure, procName, parameters);
}
#endregion
#region 查询对象、返回实体
///
/// 查询对象、返回实体
///
/// 主键值
///
public T FindEntity(object propertyValue) where T : new()
{
string pkName = DatabaseCommon.GetKeyField().ToString();//获取主键字段
StringBuilder strSql = DatabaseCommon.SelectSql(1);
strSql.Append(" AND ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
return DatabaseReader.ReaderToModel(dr);
}
///
/// 查询对象、返回实体
///
/// 实体属性名称
/// 字段值
///
public T FindEntity(string propertyName, object propertyValue) where T : new()
{
string pkName = propertyName;
StringBuilder strSql = DatabaseCommon.SelectSql(1);
strSql.Append(" AND ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
return DatabaseReader.ReaderToModel(dr);
}
///
/// 查询对象、返回实体
///
/// 条件
///
public T FindEntityByWhere(string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(1);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToModel(dr);
}
///
/// 查询对象、返回实体
///
/// 条件
/// sql语句对应参数
///
public T FindEntityByWhere(string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectSql(1);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToModel(dr);
}
///
/// 查询对象、返回实体
///
/// Sql语句
///
public T FindEntityBySql(string strSql)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToModel(dr);
}
///
/// 查询对象、返回实体
///
/// Sql语句
/// sql语句对应参数
///
public T FindEntityBySql(string strSql, DbParameter[] parameters)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToModel(dr);
}
#endregion
#region 查询对象、返回哈希表
///
/// 查询对象、返回哈希表
///
/// 表名
/// 实体属性名称
/// 字段值
///
public Hashtable FindHashtable(string tableName, string propertyName, object propertyValue)
{
StringBuilder strSql = DatabaseCommon.SelectSql(tableName, 1);
strSql.Append(" AND ").Append(propertyName).Append("=").Append(DbHelper.DbParmChar + propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
return DatabaseReader.ReaderToHashtable(dr);
}
///
/// 查询对象、返回哈希表
///
/// 表名
/// 条件
///
public Hashtable FindHashtable(string tableName, StringBuilder WhereSql)
{
StringBuilder strSql = DatabaseCommon.SelectSql(tableName, 1);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToHashtable(dr);
}
///
/// 查询对象、返回哈希表
///
/// 表名
/// 条件
/// sql语句对应参数
///
public Hashtable FindHashtable(string tableName, StringBuilder WhereSql, DbParameter[] parameters)
{
StringBuilder strSql = DatabaseCommon.SelectSql(tableName, 1);
strSql.Append(WhereSql);
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToHashtable(dr);
}
///
/// 查询对象、返回哈希表
///
/// Sql语句
///
public Hashtable FindHashtableBySql(string strSql)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
return DatabaseReader.ReaderToHashtable(dr);
}
///
/// 查询对象、返回哈希表
///
/// Sql语句
/// sql语句对应参数
///
public Hashtable FindHashtableBySql(string strSql, DbParameter[] parameters)
{
IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
return DatabaseReader.ReaderToHashtable(dr);
}
#endregion
#region 查询数据、返回条数
///
/// 查询数据、返回条数
///
///
public int FindCount() where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectCountSql();
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString()));
}
///
/// 查询数据、返回条数
/// 实体属性名称
/// 字段值
///
///
public int FindCount(string propertyName, string propertyValue) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectCountSql();
strSql.Append(" AND " + propertyName + " = " + DbHelper.DbParmChar + propertyName);
IList parameter = new List();
parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameter.ToArray()));
}
///
/// 查询数据、返回条数
///
/// 条件
///
public int FindCount(string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectCountSql();
strSql.Append(WhereSql);
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString()));
}
///
/// 查询数据、返回条数
///
/// 条件
/// sql语句对应参数
///
public int FindCount(string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectCountSql();
strSql.Append(WhereSql);
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));
}
///
/// 查询数据、返回条数
///
/// Sql语句
///
public int FindCountBySql(string strSql)
{
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql));
}
///
/// 查询数据、返回条数
///
/// Sql语句
/// sql语句对应参数
///
public int FindCountBySql(string strSql, DbParameter[] parameters)
{
return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql, parameters));
}
#endregion
#region 查询数据、返回最大数
///
/// 查询数据、返回最大数
///
/// 实体属性名称
///
public object FindMax(string propertyName) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectMaxSql(propertyName);
return DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
}
///
/// 查询数据、返回最大数
///
/// 实体属性名称
/// 条件
///
public object FindMax(string propertyName, string WhereSql) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectMaxSql(propertyName);
strSql.Append(WhereSql);
return DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
}
///
/// 查询数据、返回最大数
///
/// 实体属性名称
/// 条件
/// sql语句对应参数
///
public object FindMax(string propertyName, string WhereSql, DbParameter[] parameters) where T : new()
{
StringBuilder strSql = DatabaseCommon.SelectMaxSql(propertyName);
strSql.Append(WhereSql);
return DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
}
///
/// 查询数据、返回最大数
///
/// Sql语句
///
public object FindMaxBySql(string strSql)
{
return DbHelper.ExecuteScalar(CommandType.Text, strSql);
}
///
/// 查询数据、返回最大数
///
/// Sql语句
/// sql语句对应参数
///
public object FindMaxBySql(string strSql, DbParameter[] parameters)
{
return DbHelper.ExecuteScalar(CommandType.Text, strSql, parameters);
}
#endregion
}
}