cyq.data經常使用封裝,能夠知足平常應用。若是須要特殊的處理,好比字段+一、+二、+3這樣的操做,能夠使用ExeNonQuery執行你存放在某一處的sql語句。涉及事務操做的能夠直接在BLL層使用cyq.data來操做。其餘的下邊應該涵蓋很多啦。sql
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 }