标签:
1 /// <summary> 2 /// DAL基类 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 public abstract class BaseDAL<T> where T : class, new() 6 { 7 /// <summary> 8 /// 获取单一模型 9 /// </summary> 10 /// <param name="t">模型对象</param> 11 /// <returns></returns> 12 public T GetModel(T t) 13 { 14 string sql = GetSQLstr(t, "model"); 15 SqlParameter[] sp = GetSqlParameter(t); 16 DataTable dt = SQLHelperNew.ExecuteDataTable(sql, sp); 17 if (dt.Rows.Count > 0) 18 { 19 return DataTableToModel(dt.Rows[0]); 20 } 21 else 22 { 23 return new T(); 24 } 25 26 } 27 /// <summary> 28 /// 获取模型集合 29 /// </summary> 30 /// <param name="pageindex">当前页</param> 31 /// <param name="pageSize">每页条数</param> 32 /// <param name="orderStr">排序字符</param> 33 /// <returns></returns> 34 public List<T> GetModelList(T t, int pageindex, int pageSize, string orderStr) 35 { 36 string sql = GetSQLstrList(t, pageindex, pageSize, orderStr); 37 SqlParameter[] sp = GetSqlParameter(t); 38 DataTable dt = SQLHelperNew.ExecuteDataTable(sql, sp); 39 if (dt.Rows.Count > 0) 40 { 41 return DataTableToModelList(dt); 42 } 43 else 44 { 45 return new List<T>(); 46 } 47 48 } 49 50 /// <summary> 51 /// 添加模型 52 /// </summary> 53 /// <param name="t">模型对象</param> 54 /// <returns></returns> 55 public int AddModel(T t) 56 { 57 string sql = GetSQLstr(t, "add"); 58 SqlParameter[] sp = GetSqlParameter(t); 59 return SQLHelperNew.ExecuteNonQuery(sql, sp); 60 } 61 /// <summary> 62 /// 删除模型 63 /// </summary> 64 /// <param name="t">模型对象</param> 65 /// <returns></returns> 66 public int DeleteModel(T t) 67 { 68 string sql = GetSQLstr(t, "delete"); 69 SqlParameter[] sp = GetSqlParameter(t); 70 return SQLHelperNew.ExecuteNonQuery(sql, sp); 71 } 72 /// <summary> 73 /// 修改模型 74 /// </summary> 75 /// <param name="t">模型对象</param> 76 /// <returns></returns> 77 public int EditModel(T t) 78 { 79 string sql = GetSQLstr(t, "edit"); 80 SqlParameter[] sp = GetSqlParameter(t); 81 return SQLHelperNew.ExecuteNonQuery(sql, sp); 82 } 83 /// <summary> 84 /// 获取最大条数 85 /// </summary> 86 /// <param name="t">模型对象</param> 87 /// <returns></returns> 88 public int GetModelMaxCount(T t) 89 { 90 string sql = GetSQLstr(t, "max"); 91 SqlParameter[] sp = GetSqlParameter(t); 92 return (int)SQLHelperNew.ExecuteScalar(sql, sp); 93 } 94 95 /// <summary> 96 /// 根据数据表返回模型集合 97 /// </summary> 98 /// <param name="dt"></param> 99 /// <returns></returns> 100 public abstract List<T> DataTableToModelList(DataTable dt); 101 /// <summary> 102 /// 根据数据表返回模型 103 /// </summary> 104 /// <param name="dt"></param> 105 /// <returns></returns> 106 public abstract T DataTableToModel(DataRow dt); 107 /// <summary> 108 /// 返回SQL语句 109 /// </summary> 110 /// <returns></returns> 111 public abstract string GetSQLstr(T t, string type); 112 /// <summary> 113 /// 返回SQL语句参数 114 /// </summary> 115 /// <param name="t">模型</param> 116 /// <returns></returns> 117 public abstract SqlParameter[] GetSqlParameter(T t); 118 119 /// <summary> 120 /// 返回SQL语句 121 /// </summary> 122 /// <param name="sql">SQL语句</param> 123 /// <param name="t">模型</param> 124 /// <returns></returns> 125 protected abstract string CreateSQL(string sql, T t, string type); 126 127 /// <summary> 128 /// 获取 分页SQL字符串 129 /// </summary> 130 /// <param name="t">模型</param> 131 /// <param name="pageindex">起始项</param> 132 /// <param name="pageSize">每页条数</param> 133 /// <param name="orderStr">排序字符</param> 134 /// <returns></returns> 135 public abstract string GetSQLstrList(T t, int pageindex, int pageSize, string orderStr); 136 } 137 }
标签:
原文地址:http://www.cnblogs.com/demoC/p/5121287.html