西瓜皮

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

cyq.data常用封装,可以满足日常应用。如果需要特殊的处理,比如字段+1、+2、+3这样的操作,可以使用ExeNonQuery执行你存放在某一处的sql语句。涉及事务操作的可以直接在BLL层使用cyq.data来操作。其他的下边应该涵盖不少啦。

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Text;
  5 using System.Data;
  6 using CYQ.Data;
  7 using CYQ.Data.Table;
  8 
  9 namespace XG.BLL
 10 {
 11     public class BaseBll
 12     {
 13 
 14         #region Maction 封装
 15 
 16         /// <summary>
 17         /// 获取记录数量
 18         /// </summary>
 19         /// <param name="TableName">表名</param>
 20         /// <param name="strWhere">条件</param>
 21         /// <returns>返回记录总数 </returns>
 22         public int GetCount(string TableName, string strWhere)
 23         {
 24             int i = 0;
 25             using (MAction action = new MAction(TableName))
 26             {
 27                 i = action.GetCount(strWhere);
 28             }
 29             return i;
 30         }
 31 
 32         /// <summary>
 33         /// 是否存在指定条件的数据 
 34         /// </summary>
 35         /// <param name="TableName">表名</param>
 36         /// <param name="strWhere">条件</param>
 37         /// <returns>bool</returns>
 38         public bool Exists(string TableName, string strWhere)
 39         {
 40             bool result = false;
 41             using (MAction action = new MAction(TableName))
 42             {
 43                 result = action.Exists(strWhere);
 44             }
 45             return result;
 46         }
 47 
 48         /// <summary>
 49         /// 自动绑定前台控件
 50         /// </summary>
 51         /// <param name="page">当前页,一般为this</param>
 52         /// <param name="TableName">表名</param>
 53         /// <param name="strWhere">条件</param>
 54         public Hashtable BindPage(object page, string TableName, string strWhere)
 55         {
 56             return BindPage(page, TableName, strWhere, true);
 57         }
 58 
 59         /// <summary>
 60         /// 自动绑定前台控件
 61         /// </summary>
 62         /// <param name="page">当前页,一般为this</param>
 63         /// <param name="TableName">表名</param>
 64         /// <param name="strWhere">条件</param>
 65         /// <param name="isControlEnabled">控件是否有效</param>
 66         public Hashtable BindPage(object page, string TableName, string strWhere, bool isControlEnabled)
 67         {
 68             Hashtable ht = null;
 69             using (MAction action = new MAction(TableName))
 70             {
 71                 if (action.Fill(strWhere))
 72                 {
 73                     action.SetToAll(page, isControlEnabled);
 74                     ht = GetInfo(TableName, null, strWhere);
 75                 }
 76             }
 77             return ht;
 78         }
 79 
 80         /// <summary>
 81         /// 添加数据
 82         /// </summary>
 83         /// <param name="TableName">表名</param>
 84         /// <param name="AutoPrefix">控件前缀</param>
 85         /// <param name="OtherAutoPrefix">其他前缀</param>
 86         /// <returns>bool</returns>
 87         public bool Add(string TableName, string AutoPrefix, params string[] OtherAutoPrefix)
 88         {
 89             return Add(TableName, null, AutoPrefix, OtherAutoPrefix);
 90         }
 91 
 92         /// <summary>
 93         /// 添加数据
 94         /// </summary>
 95         /// <param name="TableName">表名</param>
 96         /// <param name="ht">手工定义添加数据</param>
 97         /// <param name="AutoPrefix">前缀</param>
 98         /// <param name="OtherAutoPrefix">其他前缀</param>
 99         /// <returns>bool</returns>
100         public bool Add(string TableName, Hashtable ht, string AutoPrefix, params string[] OtherAutoPrefix)
101         {
102             bool result = false;
103             using (MAction action = new MAction(TableName))
104             {
105                 action.SetAutoPrefix(AutoPrefix, OtherAutoPrefix);
106                 if (ht != null)
107                 {
108                     foreach (DictionaryEntry de in ht)
109                     {
110                         action.Set(de.Key, de.Value);
111                     }
112                 }
113                 result = action.Insert(true);
114             }
115             return result;
116         }
117 
118         /// <summary>
119         /// 更新数据
120         /// </summary>
121         /// <param name="TableName">表名</param>
122         /// <param name="strWhere">条件</param>
123         /// <param name="AutoPrefix">控件前缀</param>
124         /// <param name="OtherAutoPrefix">其他前缀</param>
125         /// <returns>bool</returns>
126         public bool Update(string TableName, string strWhere, string AutoPrefix, params string[] OtherAutoPrefix)
127         {
128             return Update(TableName, null, strWhere, AutoPrefix, OtherAutoPrefix);
129         }
130 
131         /// <summary>
132         /// 更新数据
133         /// </summary>
134         /// <param name="TableName">表名</param>
135         /// <param name="ht">手工定义添加数据</param>
136         /// <param name="strWhere">条件</param>
137         /// <param name="AutoPrefix">前缀</param>
138         /// <param name="OtherAutoPrefix">其他前缀</param>
139         /// <returns>bool</returns>
140         public bool Update(string TableName, Hashtable ht, string strWhere, string AutoPrefix, params string[] OtherAutoPrefix)
141         {
142             bool result = false;
143             using (MAction action = new MAction(TableName))
144             {
145                 action.SetAutoPrefix(AutoPrefix, OtherAutoPrefix);
146                 if (ht != null)
147                 {
148                     foreach (DictionaryEntry de in ht)
149                     {
150                         action.Set(de.Key, de.Value);
151                     }
152                 }
153                 result = action.Update(strWhere, true);
154             }
155             return result;
156         }
157 
158         /// <summary>
159         /// 删除数据
160         /// </summary>
161         /// <param name="TableName">表名</param>
162         /// <param name="strWhere">条件</param>
163         /// <returns>bool</returns>
164         public bool Delete(string TableName, string strWhere)
165         {
166             bool result = false;
167             using (MAction action = new MAction(TableName))
168             {
169                 result = action.Delete(strWhere);
170             }
171             return result;
172         }
173 
174         /// <summary>
175         /// 获取单条记录
176         /// </summary>
177         /// <param name="TableName">表名</param>
178         /// <param name="strWhere">条件</param>
179         /// <returns>hashtable</returns>
180         public Hashtable GetInfo(string TableName, string strWhere)
181         {
182             return GetInfo(TableName, null, strWhere);
183         }
184 
185         /// <summary>
186         /// 获取单条记录
187         /// </summary>
188         /// <param name="TableName">表名</param>
189         /// <param name="Fields">字段</param>
190         /// <param name="strWhere">条件</param>
191         /// <returns>hashtable</returns>
192         public Hashtable GetInfo(string TableName, string Fields, string strWhere)
193         {
194             Hashtable ht = null;
195             using (MAction action = new MAction(TableName))
196             {
197                 if (!string.IsNullOrEmpty(Fields))
198                 {
199                     action.SetSelectColumns(Fields.Split(','));
200                 }
201                 if (action.Fill(strWhere))
202                 {
203                     MDataRow mdr = action.Data;
204                     for (int i = 0; i < mdr.Columns.Count; i++)
205                     {
206                         string key = mdr.Columns[i].ColumnName;
207                         ht[key] = mdr[i].Value;
208                     }
209                 }
210             }
211             return ht;
212         }
213 
214         /// <summary>
215         /// 获取数据列表
216         /// </summary>
217         /// <param name="TableName">表名</param>
218         /// <param name="strWhere">条件</param>
219         /// <returns>datatable</returns>
220         public DataTable GetList(string TableName, string strWhere)
221         {
222             return GetList(TableName, null, strWhere);
223         }
224 
225         /// <summary>
226         /// 获取数据列表
227         /// </summary>
228         /// <param name="TableName">表名</param>
229         /// <param name="Fields">字段</param>
230         /// <param name="strWhere">条件</param>
231         /// <returns>datatable</returns>
232         public DataTable GetList(string TableName, string Fields, string strWhere)
233         {
234             DataTable dt = null;
235             using (MAction action = new MAction(TableName))
236             {
237                 if (!string.IsNullOrEmpty(Fields))
238                 {
239                     action.SetSelectColumns(Fields.Split(','));
240                 }
241                 dt = action.Select(strWhere).ToDataTable();
242             }
243             return dt;
244         }
245 
246         /// <summary>
247         /// 获取分页数据列表
248         /// </summary>
249         /// <param name="TableName">表名</param>
250         /// <param name="pageIndex">当前页</param>
251         /// <param name="pageSize">每页记录</param>
252         /// <param name="strWhere">条件</param>
253         /// <param name="rowCount">记录总数</param>
254         /// <returns>DataTable</returns>
255         public DataTable GetPageList(string TableName, int pageIndex, int pageSize, string strWhere, out int rowCount)
256         {
257             return GetPageList(TableName, pageIndex, pageSize, strWhere, null, out rowCount);
258         }
259 
260         /// <summary>
261         /// 获取分页数据列表
262         /// </summary>
263         /// <param name="TableName">表名</param>
264         /// <param name="pageIndex">当前页</param>
265         /// <param name="pageSize">每页记录</param>
266         /// <param name="strWhere">条件</param>
267         /// <param name="Fields">字段</param>
268         /// <param name="rowCount">记录总数</param>
269         /// <returns>DataTable</returns>
270         public DataTable GetPageList(string TableName, int pageIndex, int pageSize, string strWhere, string Fields, out int rowCount)
271         {
272             DataTable dt = null;
273             using (MAction action = new MAction(TableName))
274             {
275                 if (!string.IsNullOrEmpty(Fields))
276                 {
277                     action.SetSelectColumns(Fields.Split(','));
278                 }
279                 dt = action.Select(pageIndex, pageSize, strWhere, out rowCount).ToDataTable();
280             }
281             return dt;
282         }
283 
284         #endregion
285 
286         #region 执行sql语句
287 
288         /// <summary>
289         /// 执行sql语句
290         /// </summary>
291         /// <param name="strSql">sql</param>
292         /// <returns>返回受影响的行数[用于更新或删除]</returns>
293         public int ExeNonQuery(string strSql)
294         {
295             return ExeNonQuery(strSql, null);
296         }
297 
298         /// <summary>
299         /// 执行带参数的sql语句
300         /// </summary>
301         /// <param name="strSql">sql</param>
302         /// <param name="ht">参数-值对应的hashtable</param>
303         /// <returns>返回受影响的行数[用于更新或删除]</returns>
304         public int ExeNonQuery(string strSql, Hashtable ht)
305         {
306             int i = 0;
307             using (MProc proc = new MProc(strSql))
308             {
309                 if (ht != null)
310                 {
311                     foreach (DictionaryEntry de in ht)
312                     {
313                         proc.Set(de.Key, de.Value);
314                     }
315                 }
316                 i = proc.ExeNonQuery();
317             }
318             return i;
319         }
320 
321         /// <summary>
322         /// 执行sql语句
323         /// </summary>
324         /// <param name="strSql">sql</param>
325         /// <returns>返回首行首列值</returns>
326         public object ExeScalar(string strSql)
327         {
328             return ExeScalar(strSql, null);
329         }
330 
331         /// <summary>
332         /// 执行sql语句
333         /// </summary>
334         /// <param name="strSql">sql</param>
335         /// <param name="ht">参数hashtable</param>
336         /// <returns>返回首行首列值</returns>
337         public object ExeScalar(string strSql, Hashtable ht)
338         {
339             object obj = null;
340             using (MProc proc = new MProc(strSql))
341             {
342                 if (ht != null)
343                 {
344                     foreach (DictionaryEntry de in ht)
345                     {
346                         proc.Set(de.Key, de.Value);
347                     }
348                 }
349                 obj = proc.ExeScalar<object>();
350             }
351             return obj;
352         }
353 
354         /// <summary>
355         /// 执行sql语句
356         /// </summary>
357         /// <param name="strSql">sql</param>
358         /// <returns>datatable</returns>
359         public DataTable ExeDataTable(string strSql)
360         {
361             return ExeDataTable(strSql, null);
362         }
363 
364         /// <summary>
365         /// 执行sql语句
366         /// </summary>
367         /// <param name="strSql">sql</param>
368         /// <param name="ht">参数</param>
369         /// <returns>DataTable</returns>
370         public DataTable ExeDataTable(string strSql, Hashtable ht)
371         {
372             DataTable dt = null;
373             using (MProc proc = new MProc(strSql))
374             {
375                 if (ht != null)
376                 {
377                     foreach (DictionaryEntry de in ht)
378                     {
379                         proc.Set(de.Key, de.Value);
380                     }
381                 }
382                 dt = proc.ExeMDataTable().ToDataTable();
383             }
384             return dt;
385         }
386 
387         #endregion
388 
389     }
390 }

 

posted on 2014-01-12 01:13  西瓜皮  阅读(518)  评论(0编辑  收藏  举报