/************************************************************************************
* ${CopyRight}
* File:
* Base${DAOClassPostFix}.cs
* Description:
* 基于泛型数据访问抽象基类
* Author:
* ${Author}
* ${AuthorEmail}
* ${Online}
* Finish DateTime:
* ${Date}
* History:
*
***********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
namespace ${DAONameSpace}
{
using ${ComponentNameSpace};
/// <summary>
/// 基于泛型数据访问抽象基类,封装了基本数据访问操作CRUD
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Base${DAOClassPostFix}<T> : IBase${DAOClassPostFix}<T>
{
#region 私有字段
private XmlClassMap classMap; //实体类的映射信息
private Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>(); //实体类的属性信息
private string procedureName = "MesnacPaging"; //分页存储过程名
#endregion
#region 构造方法
public Base${DAOClassPostFix}()
{
//获取实体类T的映射信息
this.classMap = EntityMapperHandler.GetInstance().GetMapDictionary()[typeof(T).Name];
//获取实体类的属性信息
PropertyInfo[] pis = typeof(T).GetProperties();
foreach (PropertyInfo pi in pis)
{
if (this.classMap.Properties.ContainsKey(pi.Name))
{
this.properties.Add(this.classMap.Properties[pi.Name].ColumnName, pi);
}
}
}
#endregion
#region 受保护方法
/// <summary>
/// 封装数据表数据到实体集合的方法
/// </summary>
/// <param name="cmdType">命令类型</param>
/// <param name="cmdText">要执行的SQL语句或存储过程名称</param>
/// <param name="values">SQL语句或存储过程的参数列表</param>
/// <returns>返回实体类的集合</returns>
protected List<T> GetBySql(CommandType cmdType, string cmdText, SqlParameter[] values)
{
using (SqlDataReader reader = DBHelper.GetReader(DBHelper.CONSTR, cmdType, cmdText, values))
{
List<T> lst = new List<T>();
Type entityType = typeof(T);
PropertyInfo[] properties = entityType.GetProperties();
Dictionary<string, XmlClassMap> dic = EntityMapperHandler.GetInstance().GetMapDictionary();
XmlClassMap classMap = dic[entityType.Name];
while (reader.Read())
{
T entity = (T)entityType.Assembly.CreateInstance(entityType.FullName);
foreach (PropertyInfo property in properties)
{
if (property.CanWrite && classMap.Properties.ContainsKey(property.Name))
{
object value = reader[classMap.Properties[property.Name].ColumnName];
if (value != null && value != DBNull.Value)
{
property.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
reader.Close();
return lst;
}
}
#endregion
#region IBase${DAOClassPostFix}<T> 成员
/// <summary>
/// 按住键或标识列查找,只有是单字段主键(非组合键)时才按主键查找
/// </summary>
/// <param name="id">对应查找记录的主键值或标识值</param>
/// <returns>返回对应记录的实体信息</returns>
public T GetById(object id)
{
string cmdText = "select * from {0} where {1}";
string where = String.Empty;
List<SqlParameter> parameters = new List<SqlParameter>();
if (this.classMap.Ids.Keys.Count == 1)
{
//如果主键字段是一个字段则按主键字段查询
foreach (XmlPropertyMap pm in this.classMap.Ids.Values)
{
where = pm.ColumnName + "=@" + pm.ColumnName;
parameters.Add(new SqlParameter("@" + pm.ColumnName, id));
break;
}
}
else if (this.classMap.Identity != null)
{
//如果没有主键,或者主键字段不止一个,则按标识列查询
where = this.classMap.Identity.ColumnName + "=@" + this.classMap.Identity.ColumnName;
parameters.Add(new SqlParameter("@" + this.classMap.Identity.ColumnName, id));
}
else
{
//如果没有标识列,或者没有主键,或者主键字段不只一个则按表中的第一个字段查询
foreach (XmlPropertyMap pm in this.classMap.Properties.Values)
{
where = pm.ColumnName + "=@" + pm.ColumnName;
parameters.Add(new SqlParameter("@" + pm.ColumnName, id));
break;
}
}
cmdText = String.Format(cmdText, this.classMap.TableName, where);
T entity = default(T);
List<T> lst = this.GetBySql(CommandType.Text, cmdText, parameters.ToArray());
if (lst != null && lst.Count > 0) entity = lst[0];
return entity;
}
/// <summary>
/// 指定参数的查询
/// </summary>
/// <param name="values">查询参数列表,KeyValuePair的Key是字段名,KeyValuePair的Value是字段值</param>
/// <returns>返回实体类的集合</returns>
public List<T> GetListByParam(params KeyValuePair<string, object>[] values)
{
string cmdText = "select * from {0} {1}";
string where = String.Empty;
List<SqlParameter> parameters = new List<SqlParameter>();
foreach (KeyValuePair<string, object> kvp in values)
{
where = String.IsNullOrEmpty(where) ? "where " + kvp.Key + "=@" + kvp.Key : where + " and " + kvp.Key + "=@" + kvp.Key;
parameters.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
}
cmdText = String.Format(cmdText, this.classMap.TableName, where);
List<T> lst = this.GetBySql(CommandType.Text, cmdText, parameters.ToArray());
return lst;
}
/// <summary>
/// 指定参数的查询
/// </summary>
/// <param name="values">查询参数列表</param>
/// <returns>返回实体类的集合</returns>
public List<T> GetListByParam(Dictionary<string, object> values)
{
string cmdText = "select * from {0} {1}";
string where = String.Empty;
List<SqlParameter> parameters = new List<SqlParameter>();
foreach (KeyValuePair<string, object> kvp in values)
{
where = String.IsNullOrEmpty(where) ? "where " + kvp.Key + "=@" + kvp.Key : where + " and " + kvp.Key + "=@" + kvp.Key;
parameters.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
}
cmdText = String.Format(cmdText, this.classMap.TableName, where);
List<T> lst = this.GetBySql(CommandType.Text, cmdText, parameters.ToArray());
return lst;
}
/// <summary>
/// 指定参数查询并排序
/// </summary>
/// <param name="order">排序条件</param>
/// <param name="values">查询参数列表</param>
/// <returns>返回实体类的集合</returns>
public List<T> GetListOrderByParam(string order, params KeyValuePair<string, object>[] values)
{
s
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
超级代码工厂.zip (58个子文件)
System.Data.SQLite.dll 815KB
SuperCode.exe.config 473B
Data.MySQL.dll 114KB
SuperCode.Business.dll 39KB
SuperCode.Factory.dll 5KB
WeifenLuo.WinFormsUI.Docking.dll 412KB
SuperCode.application 2KB
SuperCode.pdb 100KB
SuperCode.exe.manifest 12KB
Config
Formwork.xml 210B
Servers.xml 299B
HaoZip.hzv 0B
Dictionary.xml 265B
Directory.xml 115B
Formmould.xml 1KB
SuperCode.exe 217KB
SuperCode.Data.MySql.dll 12KB
SuperCode.Data.SqlServer.pdb 22KB
SuperCode.IData.dll 5KB
SuperCode.vshost.exe.config 473B
SuperCode.IData.pdb 8KB
app.publish
SuperCode.exe 217KB
SuperCode.vshost.application 2KB
SuperCode.Data.Sqlite.dll 10KB
SuperCode.vshost.exe.manifest 12KB
SuperCode.Model.pdb 52KB
SuperCode.Model.dll 14KB
template
DBHelper.txt 5KB
BIZTemplate.txt 746B
IBaseService.txt 9KB
EntityMapperHandler.txt 6KB
IBIZTemplate.txt 264B
IBaseManager.txt 651B
BaseService.txt 37KB
MISϵͳ
BLL.txt 2KB
IDAL.txt 0B
Model.txt 0B
DAL.txt 6KB
DAOTemplate.txt 338B
IDAOTemplate.txt 264B
XmlPropertyMap.txt 2KB
BaseManager.txt 6KB
PageResult.txt 4KB
EntityClassTemplate.txt 793B
XmlClassMap.txt 2KB
PageResultSetExtractor.txt 3KB
SuperCode.Data.MySql.pdb 20KB
SuperCode.vshost.exe 22KB
SuperCode.Common.dll 43KB
ICSharpCode.TextEditor.dll 384KB
SuperCode.Business.pdb 50KB
SuperCode.Data.SqlServer.dll 15KB
SuperCode.Common.pdb 94KB
SuperCode.Factory.pdb 12KB
SuperCode.Data.Sqlite.pdb 18KB
DbHelper
DbHelper_MySql.cs 11KB
Data.Helper.cs 11KB
MySql.Data.dll 368KB
Logs
共 58 条
- 1
资源评论
playgirl5000
- 粉丝: 4
- 资源: 78
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功