DALGeneric.cs
资源名称:OASystem.rar [点击查看]
上传用户:lishan0805
上传日期:2019-12-08
资源大小:12048k
文件大小:4k
源码类别:
OA系统
开发平台:
C#
- // ===================================================================
- // 产品(COM.OA.SqlServerDAL)项目
- //====================================================================
- // wangyp @Copy Right 2006-2008
- // 文件:DALGeneric.cs
- // 项目名称:工程项目管理
- // 创建时间:2008-9-23
- // 负责人:wangyp
- // 引用System.Configuration程序集
- // 在 web.config 文件
- /*
- <appSettings>
- <add key="Entity" value="COM.OA.Entity"/>
- </appSettings>
- */
- // ===================================================================
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.Text;
- using System.Reflection;
- using COM.OA.IDAL;
- namespace COM.OA.SqlServerDAL
- {
- /// <summary>
- /// 数据访问层泛型集合对象操作
- /// </summary>
- public class DALGeneric : IDALGeneric
- {
- #region 变量
- private static readonly string _path = System.Configuration.ConfigurationManager.AppSettings["Entity"];
- #endregion
- #region 构造函数
- public DALGeneric()
- { }
- #endregion
- #region -----------实例化接口函数----------
- #region 实体对象
- /// <summary>
- /// 得到泛型实体对象
- /// </summary>
- /// <typeparam name="T">泛型</typeparam>
- /// <param name="dr">dr</param>
- /// <returns>返回指定类型的实体对象</returns>
- public T Select<T>(IDataReader dr)
- {
- Type type = typeof(T);
- string typeName = DALGeneric._path + "." + type.Name;
- T obj = (T)Assembly.Load(DALGeneric._path).CreateInstance(typeName);
- //T obj = (T)Activator.CreateInstance(type);
- PropertyInfo[] propertyInfos = type.GetProperties();
- for (int i = 0; i < dr.FieldCount; i++)
- {
- for (int j = 0; j < propertyInfos.Length; j++)
- {
- PropertyInfo propertyInfo = propertyInfos[j];
- if (dr.GetName(i).Trim().ToLower() == propertyInfo.Name.Trim().ToLower())
- {
- Type propertyType = propertyInfo.PropertyType;
- if (propertyType.IsValueType || (propertyType.IsClass && propertyType.IsSerializable && propertyType.IsSealed))
- {
- object val = dr[propertyInfo.Name];
- if (val != DBNull.Value)
- {
- propertyInfo.SetValue(obj, val, null);// => obj.propertyInfo = val;
- }
- break;
- }
- }
- }
- }
- return obj;
- }
- #endregion
- #region 方法
- /// <summary>
- /// 得到表满足查询条件记录
- /// </summary>
- /// <typeparam name="T">泛型</typeparam>
- /// <param name="commandType">命令类型</param>
- /// <param name="sqlCommand">命令</param>
- /// <param name="param">命令参数数组</param>
- /// <returns>结果集</returns>
- public IList<T> Select<T>(CommandType commandType, string sqlCommand, params SqlParameter[] param)
- {
- IList<T> list = new List<T>();
- using (SqlDataReader dr = SqlHelper.ExecuteReader(Conn.SqlConn, commandType, sqlCommand, param))
- {
- while (dr.Read())
- {
- list.Add(this.Select<T>(dr));
- }
- }
- return list;
- }
- /// <summary>
- /// 执行数据库操作
- /// </summary>
- /// <param name="commandType">命令类型</param>
- /// <param name="sqlCommand">命令</param>
- /// <param name="param">命令参数数组</param>
- /// <returns>受影响的行数</returns>
- public int Execute(CommandType commandType, string sqlCommand, params SqlParameter[] param)
- {
- int result = 0;
- result = SqlHelper.ExecuteNonQuery(Conn.SqlConn, commandType, sqlCommand, param);
- return result;
- }
- #endregion
- #endregion
- }
- }