using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection ;
using System.Data ;
using BaiChang.HealBlog.Model ;
using System.Data.SqlClient;
using BaiChang.SqlDBUtility; namespace BaiChang.Middle.Front
{
public class DataAccess<T>where T:new()
{
#region Main Part public static DataAccess<T> GetDataAccess()
{
string name = "DataAccess" + typeof(T).Name;
DataAccess<T> tObj = Utils.CurrentPage.Cache[name] as DataAccess<T>; object obj = new object();
if (tObj == null)
{
Utils.CurrentPage.Cache[name] = typeof(DataAccess < T>).Assembly.CreateInstance(typeof(DataAccess<T>).Name);
tObj = Utils.CurrentPage.Cache[name] as DataAccess<T>;
}
return tObj;
} public static string ToSetString(string name)
{
string formate = "{0}={1}";
return string.Format(formate, name, name);
} public static List<T> ToModel(SqlDataReader reader)
{
Dictionary<string, PropertyInfo> ps = new Dictionary<string, PropertyInfo>();
foreach (PropertyInfo p in typeof (T).GetProperties ())
{
ps.Add(p.Name, p);
} List<T> ms=new List<T> ();
while (reader.Read())
{
T m = new T();
object[] objs = null;
int count = reader.GetValues(objs);
int i = ;
foreach (object o in objs)
{
ps[reader.GetName(i)].SetValue(m, o, null);
i++;
}
ms.Add(m);
}
return ms;
}
#region Properties
//当前实体类型
Type entityType;
Type type
{
get
{
if(entityType ==null)
entityType = typeof(T);
return entityType;
}
} //实体的所有属性
PropertyInfo[] propertyInfos;
PropertyInfo[] pros
{
get
{
if (propertyInfos == null) propertyInfos = this.type.GetProperties();
return propertyInfos;
}
} //相对应的表名
string _tableName=string.Empty ;
string tableName
{
get
{
if (_tableName == string.Empty)
{
string className = typeof(T).Name; switch (className)
{
case "Users":
case "Role_Node_Permissions":
_tableName = "PE_" + className;
default:
_tableName = "HB_" + className;
}
}
}
}
#endregion
#endregion #region 成员方法 /// <summary>
/// 得到最大ID
/// </summary>
public int GetMaxId()
{
return DbHelperSQL.GetMaxID(pros[].Name , tableName);
} /// <summary>
/// 得到最大值
/// </summary> /// <summary>
/// 是否存在该记录
/// </summary>
public bool Exists(int ID)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select count(1) from ");
strSql.Append(tableName);
strSql.Append(" where ");
strSql.Append(ToSetString(pros[].Name));
SqlParameter[] parameters = {
new SqlParameter("@"+pros[].Name, SqlDbType.Int,)};
parameters[].Value = ID; return DbHelperSQL.Exists(strSql.ToString(), parameters);
} /// <summary>
/// 增加一条数据
/// </summary>
public int Add(T model)
{
StringBuilder strSql = new StringBuilder(); strSql .Append ("insert into ");
strSql .Append (tableName );
strSql.Append(" ("); //生成XXXvalues(XXX)
int i=;
foreach (PropertyInfo p in pros)
{
strSql .Append (p.Name );
if (i != pros.Length)
strSql.Append(",");
i++;
}
strSql.Append(") values (");
i = ;
foreach (PropertyInfo p in pros)
{
strSql.Append("@");
strSql.Append(p.Name);
if (i != pros.Length)
strSql.Append(",");
i++;
}
strSql.Append(");select @@IDENTITY"); //生成sqlparameters
List<SqlParameter> sqlpars = new List<SqlParameter>();
foreach (PropertyInfo p in pros)
{
SqlParameter sp = new SqlParameter("@" + p.Name, p.GetValue(model, null));
sqlpars.Add(sp);
}
object obj = DbHelperSQL.GetSingle(strSql.ToString(), sqlpars .ToArray ());
if (obj == null)
{
return ;
}
else
{
return Convert.ToInt32(obj);
}
} /// <summary>
/// 更新一条数据
/// </summary>
public void Update(T model)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update ");
strSql .Append (tableName );
strSql .Append (" set ");
int i=;
foreach (PropertyInfo p in pros)
{
strSql.Append(ToSetString(p.Name));
if(i!=pros.Length )
strSql .Append (",");
i++;
}
strSql.Append(" where ");
strSql.Append(ToSetString(pros[].Name));
//生成sqlparameters
List<SqlParameter> sqlpars = new List<SqlParameter>();
foreach (PropertyInfo p in pros)
{
SqlParameter sp = new SqlParameter("@" + p.Name, p.GetValue(model, null));
sqlpars.Add(sp);
} DbHelperSQL.ExecuteSql(strSql.ToString(), sqlpars .ToArray());
} /// <summary>
/// 删除一条数据
/// </summary>
public void Delete(int ID)
{ StringBuilder strSql = new StringBuilder();
strSql.Append("delete from ");
strSql.Append(tableName);
strSql.Append(" where ");
strSql.Append(ToSetString(pros[].Name));
SqlParameter[] parameters = {
new SqlParameter("@"+pros [].Name, SqlDbType.Int,)};
parameters[].Value = ID; DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
} /// <summary>
/// 得到一个对象实体
/// </summary>
public T GetModel(int ID)
{ StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 ");
int i=;
foreach (PropertyInfo p in pros)
{
strSql.Append(ToSetString(p.Name));
if(i!=pros.Length )
strSql .Append (",");
}
strSql .Append (tableName );
strSql.Append(" where ");
strSql.Append(ToSetString(pros[].Name));
SqlParameter[] parameters = {
new SqlParameter("@"+pros[].Name, SqlDbType.Int,)};
parameters[].Value = ID;
List<T> ms;
using (SqlDataReader reader = DbHelperSQL.ExecuteReader(strSql.ToString (), parameters))
{
ms= ToModel(reader);
reader.Close();
}
if (ms.Count > ) return ms[];
else return default (T);
} /// <summary>
/// 获得数据列表
/// </summary>
public List<T> GetList(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM ");
strSql.Append(tableName);
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
} List<T> ms;
using (SqlDataReader reader = DbHelperSQL.ExecuteReader(strSql.ToString ()))
{
ms = ToModel(reader);
reader.Close();
}
if (ms.Count > ) return ms;
else return null;
} /// <summary>
/// 获得前几行数据
/// </summary>
public List<T> GetList(int Top, string strWhere, string filedOrder)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
if (Top > )
{
strSql.Append(" top " + Top.ToString());
}
strSql.Append(" * ");
strSql.Append(" FROM ");
strSql.Append(tableName);
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
strSql.Append(" order by " + filedOrder);
List<T> ms;
using (SqlDataReader reader = DbHelperSQL.ExecuteReader(strSql.ToString ()))
{
ms = ToModel(reader);
reader.Close();
}
if (ms.Count > ) return ms;
else return null;
} ///<summary>
///分页获取数据列表
///</summary>
public List<T> GetList(int PageSize,int PageIndex,string strWhere)
{
List<T> ms=GetList(strWhere);
int startIndex=, endIndex=;
Utils.GetPageArea(ref startIndex, ref endIndex, PageIndex, PageSize, ms.Count);
List<T> result = new List<T>();
for (int i = startIndex; i <= endIndex; i++)
{
result.Add(ms[i]);
}
return result;
} #endregion 成员方法 }
}

一点想法+一点尝试
还有些问题有待解决^_^

作者:today4king

转载:http://www.cnblogs.com/jinzhao/archive/2009/07/24/1530213.html

最新文章

  1. Java IO7:管道流、对象流
  2. Map集合 总结
  3. Python 学习笔记9(装饰器,decorator)
  4. HDU 2899 Strange fuction 【三分】
  5. JavaScript---闭包和作用域链
  6. 淘宝(阿里百川)手机客户端开发日记第四篇 自定义ListView详解
  7. Scanner类的使用
  8. PHP过滤HTML标签的三种方法
  9. Lua基础之语法
  10. 【转】Hibernate各种主键生成策略与配置详解
  11. Delphi 编写的Web Service
  12. 浅谈JNDI的使用
  13. 国际化之Android设备支持的语种
  14. LOJ-10100(割点个数)
  15. centos7.2环境下安装smokeping对网络状态进行监控
  16. .net图表之ECharts随笔04-散点图
  17. IBM MQ + WebSphere + Spring JMS配置方法
  18. Effective C++ Placement new
  19. (数据挖掘-入门-6)十折交叉验证和K近邻
  20. [svc][op]LVS+keepalived

热门文章

  1. vue.js 第五课
  2. 如何利用git由本机向github上传文件
  3. Android5.0新特性——全新的动画(animation)
  4. Ubuntu 设置Vim tab为四个空格
  5. 【c++】中文设置
  6. DIY时钟类--广州百田笔试之一
  7. 两款web api 调试工具
  8. WebView使用详解(一)——Native与JS相互调用(附JadX反编译)
  9. Improving the AbiWord&#39;s Piece Table
  10. Js继承小结
  11. 使用jQuery中trigger()方法自动触发事件
  12. hadoop配置遇到问题的解决
  13. 【Saltstack】Saltstack简单说明
  14. hdu1789 Doing Homework again---(经典贪心)
  15. 微信小程序之获取用户位置权限(拒绝后提醒)
  16. Jquery消息提示插件toastr使用详解
  17. Java_Object
  18. oracle表空间自增
  19. vue特殊属性 key ref slot
  20. HTTP协议04-返回状态码