纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1536 lines
66 KiB

3 years ago
  1. using NFine.Data.Extensions;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace NFine.Repository
  11. {
  12. public class Database : IDatabase, IDisposable
  13. {
  14. #region 构造函数
  15. public static string connString { get; set; }
  16. /// <summary>
  17. /// 构造方法
  18. /// </summary>
  19. public Database(string connstring)
  20. {
  21. DbHelper dbhelper = new DbHelper(connstring);
  22. }
  23. /// <summary>
  24. /// 数据库连接对象
  25. /// </summary>
  26. private DbConnection dbConnection { get; set; }
  27. /// <summary>
  28. /// 事务对象
  29. /// </summary>
  30. private DbTransaction isOpenTrans { get; set; }
  31. /// <summary>
  32. /// 是否已在事务之中
  33. /// </summary>
  34. public bool inTransaction { get; set; }
  35. /// <summary>
  36. /// 事务开始
  37. /// </summary>
  38. /// <returns></returns>
  39. public DbTransaction BeginTrans()
  40. {
  41. if (!this.inTransaction)
  42. {
  43. dbConnection = DbFactory.CreateDbConnection(DbHelper.ConnectionString);
  44. if (dbConnection.State == ConnectionState.Closed)
  45. {
  46. dbConnection.Open();
  47. }
  48. inTransaction = true;
  49. isOpenTrans = dbConnection.BeginTransaction();
  50. }
  51. return isOpenTrans;
  52. }
  53. /// <summary>
  54. /// 提交事务
  55. /// </summary>
  56. public void Commit()
  57. {
  58. if (this.inTransaction)
  59. {
  60. this.inTransaction = false;
  61. this.isOpenTrans.Commit();
  62. this.Close();
  63. }
  64. }
  65. /// <summary>
  66. /// 回滚事务
  67. /// </summary>
  68. public void Rollback()
  69. {
  70. if (this.inTransaction)
  71. {
  72. this.inTransaction = false;
  73. this.isOpenTrans.Rollback();
  74. this.Close();
  75. }
  76. }
  77. /// <summary>
  78. /// 关闭数据库连接
  79. /// </summary>
  80. public void Close()
  81. {
  82. if (this.dbConnection != null)
  83. {
  84. this.dbConnection.Close();
  85. this.dbConnection.Dispose();
  86. }
  87. if (this.isOpenTrans != null)
  88. {
  89. this.isOpenTrans.Dispose();
  90. }
  91. this.dbConnection = null;
  92. this.isOpenTrans = null;
  93. }
  94. /// <summary>
  95. /// 内存回收
  96. /// </summary>
  97. public void Dispose()
  98. {
  99. if (this.dbConnection != null)
  100. {
  101. this.dbConnection.Dispose();
  102. }
  103. if (this.isOpenTrans != null)
  104. {
  105. this.isOpenTrans.Dispose();
  106. }
  107. }
  108. #endregion
  109. #region SqlBulkCopy大批量数据插入
  110. /// <summary>
  111. /// 大批量数据插入
  112. /// </summary>
  113. /// <param name="datatable">资料表</param>
  114. /// <returns></returns>
  115. public bool BulkInsert(DataTable datatable)
  116. {
  117. return false;
  118. }
  119. #endregion
  120. #region 执行SQL语句
  121. /// <summary>
  122. /// 执行SQL语句
  123. /// </summary>
  124. /// <param name="strSql">Sql语句</param>
  125. /// <returns></returns>
  126. public int ExecuteBySql(StringBuilder strSql)
  127. {
  128. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString());
  129. }
  130. /// <summary>
  131. /// 执行SQL语句
  132. /// </summary>
  133. /// <param name="strSql">Sql语句</param>
  134. /// <param name="isOpenTrans">事务对象</param>
  135. /// <returns></returns>
  136. public int ExecuteBySql(StringBuilder strSql, DbTransaction isOpenTrans)
  137. {
  138. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString());
  139. }
  140. /// <summary>
  141. /// 执行SQL语句
  142. /// </summary>
  143. /// <param name="strSql">Sql语句</param>
  144. /// <param name="parameters">sql语句对应参数</param>
  145. /// <returns></returns>
  146. public int ExecuteBySql(StringBuilder strSql, DbParameter[] parameters)
  147. {
  148. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
  149. }
  150. /// <summary>
  151. /// 执行SQL语句
  152. /// </summary>
  153. /// <param name="strSql">Sql语句</param>
  154. /// <param name="parameters">sql语句对应参数</param>
  155. /// <param name="isOpenTrans">事务对象</param>
  156. /// <returns></returns>
  157. public int ExecuteBySql(StringBuilder strSql, DbParameter[] parameters, DbTransaction isOpenTrans)
  158. {
  159. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameters);
  160. }
  161. #endregion
  162. #region 执行存储过程
  163. /// <summary>
  164. /// 执行存储过程
  165. /// </summary>
  166. /// <param name="procName">存储过程</param>
  167. /// <returns></returns>
  168. public int ExecuteByProc(string procName)
  169. {
  170. return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, procName);
  171. }
  172. /// <summary>
  173. /// 执行存储过程
  174. /// </summary>
  175. /// <param name="procName">存储过程</param>
  176. /// <param name="isOpenTrans">事务对象</param>
  177. /// <returns></returns>
  178. public int ExecuteByProc(string procName, DbTransaction isOpenTrans)
  179. {
  180. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.StoredProcedure, procName);
  181. }
  182. /// <summary>
  183. /// 执行存储过程
  184. /// </summary>
  185. /// <param name="procName">存储过程</param>
  186. /// <param name="parameters">sql语句对应参数</param>
  187. /// <returns></returns>
  188. public int ExecuteByProc(string procName, DbParameter[] parameters)
  189. {
  190. return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, procName, parameters);
  191. }
  192. /// <summary>
  193. /// 执行存储过程
  194. /// </summary>
  195. /// <param name="procName">存储过程</param>
  196. /// <param name="parameters">sql语句对应参数</param>
  197. /// <param name="isOpenTrans">事务对象</param>
  198. /// <returns></returns>
  199. public int ExecuteByProc(string procName, DbParameter[] parameters, DbTransaction isOpenTrans)
  200. {
  201. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.StoredProcedure, procName, parameters);
  202. }
  203. #endregion
  204. #region 插入数据
  205. /// <summary>
  206. /// 插入数据
  207. /// </summary>
  208. /// <param name="entity">实体类对象</param>
  209. /// <returns></returns>
  210. public int Insert<T>(T entity)
  211. {
  212. object val = 0;
  213. StringBuilder strSql = DatabaseCommon.InsertSql<T>(entity);
  214. DbParameter[] parameter = DatabaseCommon.GetParameter<T>(entity);
  215. val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
  216. return Convert.ToInt32(val);
  217. }
  218. /// <summary>
  219. /// 插入数据
  220. /// </summary>
  221. /// <param name="entity">实体类对象</param>
  222. /// <param name="isOpenTrans">事务对象</param>
  223. /// <returns></returns>
  224. public int Insert<T>(T entity, DbTransaction isOpenTrans)
  225. {
  226. object val = 0;
  227. StringBuilder strSql = DatabaseCommon.InsertSql<T>(entity);
  228. DbParameter[] parameter = DatabaseCommon.GetParameter<T>(entity);
  229. val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
  230. return Convert.ToInt32(val);
  231. }
  232. /// <summary>
  233. /// 批量插入数据
  234. /// </summary>
  235. /// <param name="entity">实体类对象</param>
  236. /// <returns></returns>
  237. public int Insert<T>(List<T> entity)
  238. {
  239. object val = 0;
  240. DbTransaction isOpenTrans = this.BeginTrans();
  241. try
  242. {
  243. foreach (var item in entity)
  244. {
  245. this.Insert<T>(item, isOpenTrans);
  246. }
  247. this.Commit();
  248. val = 1;
  249. }
  250. catch (Exception ex)
  251. {
  252. this.Rollback();
  253. this.Close();
  254. val = -1;
  255. throw ex;
  256. }
  257. return Convert.ToInt32(val);
  258. }
  259. /// <summary>
  260. /// 批量插入数据
  261. /// </summary>
  262. /// <param name="entity">实体类对象</param>
  263. /// <param name="isOpenTrans">事务对象</param>
  264. /// <returns></returns>
  265. public int Insert<T>(List<T> entity, DbTransaction isOpenTrans)
  266. {
  267. object val = 0;
  268. try
  269. {
  270. foreach (var item in entity)
  271. {
  272. this.Insert<T>(item, isOpenTrans);
  273. }
  274. val = 1;
  275. }
  276. catch (Exception ex)
  277. {
  278. val = -1;
  279. throw ex;
  280. }
  281. return Convert.ToInt32(val);
  282. }
  283. /// <summary>
  284. /// 插入数据
  285. /// </summary>
  286. /// <param name="tableName">表名</param>
  287. /// <param name="ht">哈希表键值</param>
  288. /// <returns></returns>
  289. public int Insert(string tableName, Hashtable ht)
  290. {
  291. object val = 0;
  292. StringBuilder strSql = DatabaseCommon.InsertSql(tableName, ht);
  293. DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
  294. val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
  295. return Convert.ToInt32(val);
  296. }
  297. /// <summary>
  298. /// 插入数据
  299. /// </summary>
  300. /// <param name="tableName">表名</param>
  301. /// <param name="ht">哈希表键值</param>
  302. /// <param name="isOpenTrans">事务对象</param>
  303. /// <returns></returns>
  304. public int Insert(string tableName, Hashtable ht, DbTransaction isOpenTrans)
  305. {
  306. object val = 0;
  307. StringBuilder strSql = DatabaseCommon.InsertSql(tableName, ht);
  308. DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
  309. val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
  310. return Convert.ToInt32(val);
  311. }
  312. #endregion
  313. #region 修改数据
  314. /// <summary>
  315. /// 修改数据
  316. /// </summary>
  317. /// <param name="entity">实体对象</param>
  318. /// <returns></returns>
  319. public int Update<T>(T entity)
  320. {
  321. object val = 0;
  322. StringBuilder strSql = DatabaseCommon.UpdateSql<T>(entity);
  323. DbParameter[] parameter = DatabaseCommon.GetParameter<T>(entity);
  324. val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
  325. return Convert.ToInt32(val);
  326. }
  327. /// <summary>
  328. /// 修改数据
  329. /// </summary>
  330. /// <param name="entity">实体对象</param>
  331. /// <param name="isOpenTrans">事务对象</param>
  332. /// <returns></returns>
  333. public int Update<T>(T entity, DbTransaction isOpenTrans)
  334. {
  335. object val = 0;
  336. StringBuilder strSql = DatabaseCommon.UpdateSql<T>(entity);
  337. DbParameter[] parameter = DatabaseCommon.GetParameter<T>(entity);
  338. val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
  339. return Convert.ToInt32(val);
  340. }
  341. /// <summary>
  342. /// 修改数据
  343. /// </summary>
  344. /// <param name="propertyName">实体属性名称</param>
  345. /// <param name="propertyValue">字段值</param>
  346. /// <returns></returns>
  347. public int Update<T>(string propertyName, string propertyValue)
  348. {
  349. object val = 0;
  350. StringBuilder strSql = new StringBuilder();
  351. StringBuilder sb = new StringBuilder();
  352. sb.Append("Update ");
  353. sb.Append(typeof(T).Name);
  354. sb.Append(" Set ");
  355. sb.Append(propertyName);
  356. sb.Append("=");
  357. sb.Append(DbHelper.DbParmChar + propertyName);
  358. IList<DbParameter> parameter = new List<DbParameter>();
  359. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  360. val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
  361. return Convert.ToInt32(val);
  362. }
  363. /// <summary>
  364. /// 修改数据
  365. /// </summary>
  366. /// <param name="propertyName">实体属性名称</param>
  367. /// <param name="propertyValue">字段值</param>
  368. /// <param name="isOpenTrans">事务对象</param>
  369. /// <returns></returns>
  370. public int Update<T>(string propertyName, string propertyValue, DbTransaction isOpenTrans)
  371. {
  372. object val = 0;
  373. StringBuilder strSql = new StringBuilder();
  374. StringBuilder sb = new StringBuilder();
  375. sb.Append("Update ");
  376. sb.Append(typeof(T).Name);
  377. sb.Append(" Set ");
  378. sb.Append(propertyName);
  379. sb.Append("=");
  380. sb.Append(DbHelper.DbParmChar + propertyName);
  381. IList<DbParameter> parameter = new List<DbParameter>();
  382. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  383. val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
  384. return Convert.ToInt32(val);
  385. }
  386. /// <summary>
  387. /// 批量修改数据
  388. /// </summary>
  389. /// <param name="entity">实体对象</param>
  390. /// <returns></returns>
  391. public int Update<T>(List<T> entity)
  392. {
  393. object val = 0;
  394. DbTransaction isOpenTrans = this.BeginTrans();
  395. try
  396. {
  397. foreach (var item in entity)
  398. {
  399. this.Update<T>(item, isOpenTrans);
  400. }
  401. this.Commit();
  402. val = 1;
  403. }
  404. catch (Exception ex)
  405. {
  406. this.Rollback();
  407. this.Close();
  408. val = -1;
  409. throw ex;
  410. }
  411. return Convert.ToInt32(val);
  412. }
  413. /// <summary>
  414. /// 批量修改数据
  415. /// </summary>
  416. /// <param name="entity">实体对象</param>
  417. /// <param name="isOpenTrans">事务对象</param>
  418. /// <returns></returns>
  419. public int Update<T>(List<T> entity, DbTransaction isOpenTrans)
  420. {
  421. object val = 0;
  422. try
  423. {
  424. foreach (var item in entity)
  425. {
  426. this.Update<T>(item, isOpenTrans);
  427. }
  428. val = 1;
  429. }
  430. catch (Exception ex)
  431. {
  432. val = -1;
  433. throw ex;
  434. }
  435. return Convert.ToInt32(val);
  436. }
  437. /// <summary>
  438. /// 修改数据
  439. /// </summary>
  440. /// <param name="tableName">表名</param>
  441. /// <param name="ht">哈希表键值</param>
  442. /// <param name="propertyName">主键字段</param>
  443. /// <returns></returns>
  444. public int Update(string tableName, Hashtable ht, string propertyName)
  445. {
  446. object val = 0;
  447. StringBuilder strSql = DatabaseCommon.UpdateSql(tableName, ht, propertyName);
  448. DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
  449. val = DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
  450. return Convert.ToInt32(val);
  451. }
  452. /// <summary>
  453. /// 修改数据
  454. /// </summary>
  455. /// <param name="tableName">表名</param>
  456. /// <param name="ht">哈希表键值</param>
  457. /// <param name="propertyName">主键字段</param>
  458. /// <param name="isOpenTrans">事务对象</param>
  459. /// <returns></returns>
  460. public int Update(string tableName, Hashtable ht, string propertyName, DbTransaction isOpenTrans)
  461. {
  462. object val = 0;
  463. StringBuilder strSql = DatabaseCommon.UpdateSql(tableName, ht, propertyName);
  464. DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
  465. val = DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
  466. return Convert.ToInt32(val);
  467. }
  468. #endregion
  469. #region 删除数据
  470. /// <summary>
  471. /// 删除数据
  472. /// </summary>
  473. /// <param name="entity">实体类</param>
  474. /// <returns></returns>
  475. public int Delete<T>(T entity)
  476. {
  477. StringBuilder strSql = DatabaseCommon.DeleteSql(entity);
  478. DbParameter[] parameter = DatabaseCommon.GetParameter<T>(entity);
  479. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
  480. }
  481. /// <summary>
  482. /// 删除数据
  483. /// </summary>
  484. /// <param name="entity">实体类</param>
  485. /// <param name="isOpenTrans">事务对象</param>
  486. /// <returns></returns>
  487. public int Delete<T>(T entity, DbTransaction isOpenTrans)
  488. {
  489. StringBuilder strSql = DatabaseCommon.DeleteSql(entity);
  490. DbParameter[] parameter = DatabaseCommon.GetParameter<T>(entity);
  491. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
  492. }
  493. /// <summary>
  494. /// 删除数据
  495. /// </summary>
  496. /// <param name="propertyValue">主键值</param>
  497. /// <returns></returns>
  498. public int Delete<T>(object propertyValue)
  499. {
  500. string tableName = typeof(T).Name;//获取表名
  501. string pkName = DatabaseCommon.GetKeyField<T>().ToString();//获取主键
  502. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, pkName);
  503. IList<DbParameter> parameter = new List<DbParameter>();
  504. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
  505. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
  506. }
  507. /// <summary>
  508. /// 删除数据
  509. /// </summary>
  510. /// <param name="propertyValue">主键值</param>
  511. /// <param name="isOpenTrans">事务对象</param>
  512. /// <returns></returns>
  513. public int Delete<T>(object propertyValue, DbTransaction isOpenTrans)
  514. {
  515. string tableName = typeof(T).Name;//获取表名
  516. string pkName = DatabaseCommon.GetKeyField<T>().ToString();//获取主键
  517. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, pkName);
  518. IList<DbParameter> parameter = new List<DbParameter>();
  519. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
  520. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
  521. }
  522. /// <summary>
  523. /// 删除数据
  524. /// </summary>
  525. /// <param name="propertyName">实体属性名称</param>
  526. /// <param name="propertyValue">字段值</param>
  527. /// <returns></returns>
  528. public int Delete<T>(string propertyName, string propertyValue)
  529. {
  530. string tableName = typeof(T).Name;//获取表名
  531. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
  532. IList<DbParameter> parameter = new List<DbParameter>();
  533. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  534. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
  535. }
  536. /// <summary>
  537. /// 删除数据
  538. /// </summary>
  539. /// <param name="propertyName">实体属性名称</param>
  540. /// <param name="propertyValue">字段值</param>
  541. /// <param name="isOpenTrans">事务对象</param>
  542. /// <returns></returns>
  543. public int Delete<T>(string propertyName, string propertyValue, DbTransaction isOpenTrans)
  544. {
  545. string tableName = typeof(T).Name;//获取表名
  546. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
  547. IList<DbParameter> parameter = new List<DbParameter>();
  548. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  549. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
  550. }
  551. /// <summary>
  552. /// 删除数据
  553. /// </summary>
  554. /// <param name="tableName">表名</param>
  555. /// <param name="propertyName">实体属性名称</param>
  556. /// <param name="propertyValue">字段值</param>
  557. /// <returns></returns>
  558. public int Delete(string tableName, string propertyName, string propertyValue)
  559. {
  560. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
  561. IList<DbParameter> parameter = new List<DbParameter>();
  562. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  563. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray());
  564. }
  565. /// <summary>
  566. /// 删除数据
  567. /// </summary>
  568. /// <param name="tableName">表名</param>
  569. /// <param name="propertyName">实体属性名称</param>
  570. /// <param name="propertyValue">字段值</param>
  571. /// <param name="isOpenTrans">事务对象</param>
  572. /// <returns></returns>
  573. public int Delete(string tableName, string propertyName, string propertyValue, DbTransaction isOpenTrans)
  574. {
  575. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, propertyName);
  576. IList<DbParameter> parameter = new List<DbParameter>();
  577. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  578. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray());
  579. }
  580. /// <summary>
  581. /// 删除数据
  582. /// </summary>
  583. /// <param name="tableName">表名</param>
  584. /// <param name="ht">键值生成SQL条件</param>
  585. /// <returns></returns>
  586. public int Delete(string tableName, Hashtable ht)
  587. {
  588. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, ht);
  589. DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
  590. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter);
  591. }
  592. /// <summary>
  593. /// 删除数据
  594. /// </summary>
  595. /// <param name="tableName">表名</param>
  596. /// <param name="ht">键值生成SQL条件</param>
  597. /// <param name="isOpenTrans">事务对象</param>
  598. /// <returns></returns>
  599. public int Delete(string tableName, Hashtable ht, DbTransaction isOpenTrans)
  600. {
  601. StringBuilder strSql = DatabaseCommon.DeleteSql(tableName, ht);
  602. DbParameter[] parameter = DatabaseCommon.GetParameter(ht);
  603. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter);
  604. }
  605. /// <summary>
  606. /// 批量删除数据
  607. /// </summary>
  608. /// <param name="propertyValue">主键值:数组1,2,3,4,5,6.....</param>
  609. /// <returns></returns>
  610. public int Delete<T>(object[] propertyValue)
  611. {
  612. string tableName = typeof(T).Name;//获取表名
  613. string pkName = DatabaseCommon.GetKeyField<T>().ToString();//获取主键
  614. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + pkName + " IN (");
  615. try
  616. {
  617. IList<DbParameter> parameter = new List<DbParameter>();
  618. int index = 0;
  619. string str = DbHelper.DbParmChar + "ID" + index;
  620. for (int i = 0; i < (propertyValue.Length - 1); i++)
  621. {
  622. object obj2 = propertyValue[i];
  623. str = DbHelper.DbParmChar + "ID" + index;
  624. strSql.Append(str).Append(",");
  625. parameter.Add(DbFactory.CreateDbParameter(str, obj2));
  626. index++;
  627. }
  628. str = DbHelper.DbParmChar + "ID" + index;
  629. strSql.Append(str);
  630. parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
  631. strSql.Append(")");
  632. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
  633. }
  634. catch (Exception ex)
  635. {
  636. throw ex;
  637. }
  638. }
  639. /// <summary>
  640. /// 批量删除数据
  641. /// </summary>
  642. /// <param name="propertyValue">主键值:数组1,2,3,4,5,6.....</param>
  643. /// <param name="isOpenTrans">事务对象</param>
  644. /// <returns></returns>
  645. public int Delete<T>(object[] propertyValue, DbTransaction isOpenTrans)
  646. {
  647. string tableName = typeof(T).Name;//获取表名
  648. string pkName = DatabaseCommon.GetKeyField<T>().ToString();//获取主键
  649. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
  650. try
  651. {
  652. IList<DbParameter> parameter = new List<DbParameter>();
  653. int index = 0;
  654. string str = DbHelper.DbParmChar + "ID" + index;
  655. for (int i = 0; i < (propertyValue.Length - 1); i++)
  656. {
  657. object obj2 = propertyValue[i];
  658. str = DbHelper.DbParmChar + "ID" + index;
  659. strSql.Append(str).Append(",");
  660. parameter.Add(DbFactory.CreateDbParameter(str, obj2));
  661. index++;
  662. }
  663. str = DbHelper.DbParmChar + "ID" + index;
  664. strSql.Append(str);
  665. parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
  666. strSql.Append(")");
  667. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
  668. }
  669. catch (Exception ex)
  670. {
  671. throw ex;
  672. }
  673. }
  674. /// <summary>
  675. /// 批量删除数据
  676. /// </summary>
  677. /// <param name="propertyName">实体属性名称</param>
  678. /// <param name="propertyValue">字段值:数组1,2,3,4,5,6.....</param>
  679. /// <returns></returns>
  680. public int Delete<T>(string propertyName, object[] propertyValue)
  681. {
  682. string tableName = typeof(T).Name;//获取表名
  683. string pkName = propertyName;
  684. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
  685. try
  686. {
  687. IList<DbParameter> parameter = new List<DbParameter>();
  688. int index = 0;
  689. string str = DbHelper.DbParmChar + "ID" + index;
  690. for (int i = 0; i < (propertyValue.Length - 1); i++)
  691. {
  692. object obj2 = propertyValue[i];
  693. str = DbHelper.DbParmChar + "ID" + index;
  694. strSql.Append(str).Append(",");
  695. parameter.Add(DbFactory.CreateDbParameter(str, obj2));
  696. index++;
  697. }
  698. str = DbHelper.DbParmChar + "ID" + index;
  699. strSql.Append(str);
  700. parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
  701. strSql.Append(")");
  702. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
  703. }
  704. catch (Exception ex)
  705. {
  706. throw ex;
  707. }
  708. }
  709. /// <summary>
  710. /// 批量删除数据
  711. /// </summary>
  712. /// <param name="propertyName">实体属性名称</param>
  713. /// <param name="propertyValue">字段值:数组1,2,3,4,5,6.....</param>
  714. /// <param name="isOpenTrans">事务对象</param>
  715. /// <returns></returns>
  716. public int Delete<T>(string propertyName, object[] propertyValue, DbTransaction isOpenTrans)
  717. {
  718. string tableName = typeof(T).Name;//获取表名
  719. string pkName = propertyName;
  720. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
  721. try
  722. {
  723. IList<DbParameter> parameter = new List<DbParameter>();
  724. int index = 0;
  725. string str = DbHelper.DbParmChar + "ID" + index;
  726. for (int i = 0; i < (propertyValue.Length - 1); i++)
  727. {
  728. object obj2 = propertyValue[i];
  729. str = DbHelper.DbParmChar + "ID" + index;
  730. strSql.Append(str).Append(",");
  731. parameter.Add(DbFactory.CreateDbParameter(str, obj2));
  732. index++;
  733. }
  734. str = DbHelper.DbParmChar + "ID" + index;
  735. strSql.Append(str);
  736. parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
  737. strSql.Append(")");
  738. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
  739. }
  740. catch (Exception ex)
  741. {
  742. throw ex;
  743. }
  744. }
  745. /// <summary>
  746. /// 批量删除数据
  747. /// </summary>
  748. /// <param name="tableName">表名</param>
  749. /// <param name="propertyName">实体属性名称</param>
  750. /// <param name="propertyValue">字段值:数组1,2,3,4,5,6.....</param>
  751. /// <returns></returns>
  752. public int Delete(string tableName, string propertyName, object[] propertyValue)
  753. {
  754. string pkName = propertyName;
  755. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
  756. try
  757. {
  758. IList<DbParameter> parameter = new List<DbParameter>();
  759. int index = 0;
  760. string str = DbHelper.DbParmChar + "ID" + index;
  761. for (int i = 0; i < (propertyValue.Length - 1); i++)
  762. {
  763. object obj2 = propertyValue[i];
  764. str = DbHelper.DbParmChar + "ID" + index;
  765. strSql.Append(str).Append(",");
  766. parameter.Add(DbFactory.CreateDbParameter(str, obj2));
  767. index++;
  768. }
  769. str = DbHelper.DbParmChar + "ID" + index;
  770. strSql.Append(str);
  771. parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
  772. strSql.Append(")");
  773. return DbHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
  774. }
  775. catch (Exception ex)
  776. {
  777. throw ex;
  778. }
  779. }
  780. /// <summary>
  781. /// 批量删除数据
  782. /// </summary>
  783. /// <param name="tableName">表名</param>
  784. /// <param name="propertyName">实体属性名称</param>
  785. /// <param name="propertyValue">字段值:数组1,2,3,4,5,6.....</param>
  786. /// <param name="isOpenTrans">事务对象</param>
  787. /// <returns></returns>
  788. public int Delete(string tableName, string propertyName, object[] propertyValue, DbTransaction isOpenTrans)
  789. {
  790. string pkName = propertyName;
  791. StringBuilder strSql = new StringBuilder("DELETE FROM " + tableName + " WHERE " + DbHelper.DbParmChar + pkName + " IN (");
  792. try
  793. {
  794. IList<DbParameter> parameter = new List<DbParameter>();
  795. int index = 0;
  796. string str = DbHelper.DbParmChar + "ID" + index;
  797. for (int i = 0; i < (propertyValue.Length - 1); i++)
  798. {
  799. object obj2 = propertyValue[i];
  800. str = DbHelper.DbParmChar + "ID" + index;
  801. strSql.Append(str).Append(",");
  802. parameter.Add(DbFactory.CreateDbParameter(str, obj2));
  803. index++;
  804. }
  805. str = DbHelper.DbParmChar + "ID" + index;
  806. strSql.Append(str);
  807. parameter.Add(DbFactory.CreateDbParameter(str, propertyValue[index]));
  808. strSql.Append(")");
  809. return DbHelper.ExecuteNonQuery(isOpenTrans, CommandType.Text, strSql.ToString(), parameter.ToArray()); ;
  810. }
  811. catch (Exception ex)
  812. {
  813. throw ex;
  814. }
  815. }
  816. #endregion
  817. #region 查询数据列表、返回List
  818. /// <summary>
  819. /// 查询数据列表、返回List
  820. /// </summary>
  821. /// <param name="Top">显示条数</param>
  822. /// <returns></returns>
  823. public List<T> FindListTop<T>(int Top) where T : new()
  824. {
  825. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  826. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  827. return DatabaseReader.ReaderToList<T>(dr);
  828. }
  829. /// <summary>
  830. /// 查询数据列表、返回List
  831. /// </summary>
  832. /// <param name="Top">显示条数</param>
  833. /// <param name="propertyName">实体属性名称</param>
  834. /// <param name="propertyValue">字段值</param>
  835. /// <returns></returns>
  836. public List<T> FindListTop<T>(int Top, string propertyName, string propertyValue) where T : new()
  837. {
  838. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  839. strSql.Append(" AND " + propertyName + " = " + DbHelper.DbParmChar + propertyName);
  840. IList<DbParameter> parameter = new List<DbParameter>();
  841. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  842. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
  843. return DatabaseReader.ReaderToList<T>(dr);
  844. }
  845. /// <summary>
  846. /// 查询数据列表、返回List
  847. /// </summary>
  848. /// <param name="Top">显示条数</param>
  849. /// <param name="WhereSql">条件</param>
  850. /// <returns></returns>
  851. public List<T> FindListTop<T>(int Top, string WhereSql) where T : new()
  852. {
  853. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  854. strSql.Append(WhereSql);
  855. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  856. return DatabaseReader.ReaderToList<T>(dr);
  857. }
  858. /// <summary>
  859. /// 查询数据列表、返回List
  860. /// </summary>
  861. /// <param name="Top">显示条数</param>
  862. /// <param name="WhereSql">条件</param>
  863. /// <param name="parameters">sql语句对应参数</param>
  864. /// <returns></returns>
  865. public List<T> FindListTop<T>(int Top, string WhereSql, DbParameter[] parameters) where T : new()
  866. {
  867. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  868. strSql.Append(WhereSql);
  869. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  870. return DatabaseReader.ReaderToList<T>(dr);
  871. }
  872. /// <summary>
  873. /// 查询数据列表、返回List
  874. /// </summary>
  875. /// <returns></returns>
  876. public List<T> FindList<T>() where T : new()
  877. {
  878. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  879. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  880. return DatabaseReader.ReaderToList<T>(dr);
  881. }
  882. /// <summary>
  883. /// 查询数据列表、返回List
  884. /// </summary>
  885. /// <param name="propertyName">实体属性名称</param>
  886. /// <param name="propertyValue">字段值</param>
  887. /// <returns></returns>
  888. public List<T> FindList<T>(string propertyName, string propertyValue) where T : new()
  889. {
  890. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  891. strSql.Append(" AND " + propertyName + " = " + DbHelper.DbParmChar + propertyName);
  892. IList<DbParameter> parameter = new List<DbParameter>();
  893. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  894. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
  895. return DatabaseReader.ReaderToList<T>(dr);
  896. }
  897. /// <summary>
  898. /// 查询数据列表、返回List
  899. /// </summary>
  900. /// <param name="WhereSql">条件</param>
  901. /// <returns></returns>
  902. public List<T> FindList<T>(string WhereSql) where T : new()
  903. {
  904. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  905. strSql.Append(WhereSql);
  906. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  907. return DatabaseReader.ReaderToList<T>(dr);
  908. }
  909. /// <summary>
  910. /// 查询数据列表、返回List
  911. /// </summary>
  912. /// <param name="WhereSql">条件</param>
  913. /// <param name="parameters">sql语句对应参数</param>
  914. /// <returns></returns>
  915. public List<T> FindList<T>(string WhereSql, DbParameter[] parameters) where T : new()
  916. {
  917. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  918. strSql.Append(WhereSql);
  919. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  920. return DatabaseReader.ReaderToList<T>(dr);
  921. }
  922. /// <summary>
  923. /// 查询数据列表、返回List
  924. /// </summary>
  925. /// <param name="strSql">Sql语句</param>
  926. /// <returns></returns>
  927. public List<T> FindListBySql<T>(string strSql)
  928. {
  929. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  930. return DatabaseReader.ReaderToList<T>(dr);
  931. }
  932. /// <summary>
  933. /// 查询数据列表、返回List
  934. /// </summary>
  935. /// <param name="strSql">Sql语句</param>
  936. /// <param name="parameters">sql语句对应参数</param>
  937. /// <returns></returns>
  938. public List<T> FindListBySql<T>(string strSql, DbParameter[] parameters)
  939. {
  940. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  941. return DatabaseReader.ReaderToList<T>(dr);
  942. }
  943. /// <summary>
  944. /// 查询数据列表、返回List
  945. /// </summary>
  946. /// <param name="orderField">排序字段</param>
  947. /// <param name="orderType">排序类型</param>
  948. /// <param name="pageIndex">当前页</param>
  949. /// <param name="pageSize">页大小</param>
  950. /// <param name="recordCount">返回查询条数</param>
  951. /// <returns></returns>
  952. public List<T> FindListPage<T>(string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
  953. {
  954. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  955. return SqlServerHelper.GetPageList<T>(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
  956. }
  957. /// <summary>
  958. /// 查询数据列表、返回List
  959. /// </summary>
  960. /// <param name="WhereSql">条件</param>
  961. /// <param name="orderField">排序字段</param>
  962. /// <param name="orderType">排序类型</param>
  963. /// <param name="pageIndex">当前页</param>
  964. /// <param name="pageSize">页大小</param>
  965. /// <param name="recordCount">返回查询条数</param>
  966. /// <returns></returns>
  967. public List<T> FindListPage<T>(string WhereSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
  968. {
  969. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  970. strSql.Append(WhereSql);
  971. return SqlServerHelper.GetPageList<T>(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
  972. }
  973. /// <summary>
  974. /// 查询数据列表、返回List
  975. /// </summary>
  976. /// <param name="WhereSql">条件</param>
  977. /// <param name="parameters">sql语句对应参数</param>
  978. /// <param name="orderField">排序字段</param>
  979. /// <param name="orderType">排序类型</param>
  980. /// <param name="pageIndex">当前页</param>
  981. /// <param name="pageSize">页大小</param>
  982. /// <param name="recordCount">返回查询条数</param>
  983. /// <returns></returns>
  984. public List<T> FindListPage<T>(string WhereSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
  985. {
  986. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  987. strSql.Append(WhereSql);
  988. return SqlServerHelper.GetPageList<T>(strSql.ToString(), parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
  989. }
  990. /// <summary>
  991. /// 查询数据列表、返回List
  992. /// </summary>
  993. /// <param name="strSql">Sql语句</param>
  994. /// <param name="orderField">排序字段</param>
  995. /// <param name="orderType">排序类型</param>
  996. /// <param name="pageIndex">当前页</param>
  997. /// <param name="pageSize">页大小</param>
  998. /// <param name="recordCount">返回查询条数</param>
  999. /// <returns></returns>
  1000. public List<T> FindListPageBySql<T>(string strSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
  1001. {
  1002. return SqlServerHelper.GetPageList<T>(strSql, orderField, orderType, pageIndex, pageSize, ref recordCount);
  1003. }
  1004. /// <summary>
  1005. /// 查询数据列表、返回List
  1006. /// </summary>
  1007. /// <param name="strSql">Sql语句</param>
  1008. /// <param name="parameters">sql语句对应参数</param>
  1009. /// <param name="orderField">排序字段</param>
  1010. /// <param name="orderType">排序类型</param>
  1011. /// <param name="pageIndex">当前页</param>
  1012. /// <param name="pageSize">页大小</param>
  1013. /// <param name="recordCount">返回查询条数</param>
  1014. /// <returns></returns>
  1015. public List<T> FindListPageBySql<T>(string strSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
  1016. {
  1017. return SqlServerHelper.GetPageList<T>(strSql, parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
  1018. }
  1019. #endregion
  1020. #region 查询数据列表、返回DataTable
  1021. /// <summary>
  1022. /// 查询数据列表、返回 DataTable
  1023. /// </summary>
  1024. /// <param name="Top">显示条数</param>
  1025. /// <returns></returns>
  1026. public DataTable FindTableTop<T>(int Top) where T : new()
  1027. {
  1028. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  1029. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1030. return DatabaseReader.ReaderToDataTable(dr);
  1031. }
  1032. /// <summary>
  1033. /// 查询数据列表、返回 DataTable
  1034. /// </summary>
  1035. /// <param name="Top">显示条数</param>
  1036. /// <param name="WhereSql">条件</param>
  1037. /// <returns></returns>
  1038. public DataTable FindTableTop<T>(int Top, string WhereSql) where T : new()
  1039. {
  1040. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  1041. strSql.Append(WhereSql);
  1042. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1043. return DatabaseReader.ReaderToDataTable(dr);
  1044. }
  1045. /// <summary>
  1046. /// 查询数据列表、返回 DataTable
  1047. /// </summary>
  1048. /// <param name="Top">显示条数</param>
  1049. /// <param name="WhereSql">条件</param>
  1050. /// <param name="parameters">sql语句对应参数</param>
  1051. /// <returns></returns>
  1052. public DataTable FindTableTop<T>(int Top, string WhereSql, DbParameter[] parameters) where T : new()
  1053. {
  1054. StringBuilder strSql = DatabaseCommon.SelectSql<T>(Top);
  1055. strSql.Append(WhereSql);
  1056. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1057. return DatabaseReader.ReaderToDataTable(dr);
  1058. }
  1059. /// <summary>
  1060. /// 查询数据列表、返回 DataTable
  1061. /// </summary>
  1062. /// <returns></returns>
  1063. public DataTable FindTable<T>() where T : new()
  1064. {
  1065. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  1066. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1067. return DatabaseReader.ReaderToDataTable(dr);
  1068. }
  1069. /// <summary>
  1070. /// 查询数据列表、返回 DataTable
  1071. /// </summary>
  1072. /// <param name="WhereSql">条件</param>
  1073. /// <returns></returns>
  1074. public DataTable FindTable<T>(string WhereSql) where T : new()
  1075. {
  1076. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  1077. strSql.Append(WhereSql);
  1078. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1079. return DatabaseReader.ReaderToDataTable(dr);
  1080. }
  1081. /// <summary>
  1082. /// 查询数据列表、返回 DataTable
  1083. /// </summary>
  1084. /// <param name="WhereSql">条件</param>
  1085. /// <param name="parameters">sql语句对应参数</param>
  1086. /// <returns></returns>
  1087. public DataTable FindTable<T>(string WhereSql, DbParameter[] parameters) where T : new()
  1088. {
  1089. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  1090. strSql.Append(WhereSql);
  1091. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1092. return DatabaseReader.ReaderToDataTable(dr);
  1093. }
  1094. /// <summary>
  1095. /// 查询数据列表、返回 DataTable
  1096. /// </summary>
  1097. /// <param name="strSql">Sql语句</param>
  1098. /// <returns></returns>
  1099. public DataTable FindTableBySql(string strSql)
  1100. {
  1101. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1102. return DatabaseReader.ReaderToDataTable(dr);
  1103. }
  1104. /// <summary>
  1105. /// 查询数据列表、返回 DataTable
  1106. /// </summary>
  1107. /// <param name="strSql">Sql语句</param>
  1108. /// <param name="parameters">sql语句对应参数</param>
  1109. /// <returns></returns>
  1110. public DataTable FindTableBySql(string strSql, DbParameter[] parameters)
  1111. {
  1112. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1113. return DatabaseReader.ReaderToDataTable(dr);
  1114. }
  1115. /// <summary>
  1116. /// 查询数据列表、返回 DataTable
  1117. /// </summary>
  1118. /// <param name="orderField">排序字段</param>
  1119. /// <param name="orderType">排序类型</param>
  1120. /// <param name="pageIndex">当前页</param>
  1121. /// <param name="pageSize">页大小</param>
  1122. /// <param name="recordCount">返回查询条数</param>
  1123. /// <returns></returns>
  1124. public DataTable FindTablePage<T>(string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
  1125. {
  1126. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  1127. return SqlServerHelper.GetPageTable(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
  1128. }
  1129. /// <summary>
  1130. /// 查询数据列表、返回 DataTable
  1131. /// </summary>
  1132. /// <param name="WhereSql">条件</param>
  1133. /// <param name="orderField">排序字段</param>
  1134. /// <param name="orderType">排序类型</param>
  1135. /// <param name="pageIndex">当前页</param>
  1136. /// <param name="pageSize">页大小</param>
  1137. /// <param name="recordCount">返回查询条数</param>
  1138. /// <returns></returns>
  1139. public DataTable FindTablePage<T>(string WhereSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
  1140. {
  1141. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  1142. strSql.Append(WhereSql);
  1143. return SqlServerHelper.GetPageTable(strSql.ToString(), orderField, orderType, pageIndex, pageSize, ref recordCount);
  1144. }
  1145. /// <summary>
  1146. /// 查询数据列表、返回 DataTable
  1147. /// </summary>
  1148. /// <param name="WhereSql">条件</param>
  1149. /// <param name="parameters">sql语句对应参数</param>
  1150. /// <param name="orderField">排序字段</param>
  1151. /// <param name="orderType">排序类型</param>
  1152. /// <param name="pageIndex">当前页</param>
  1153. /// <param name="pageSize">页大小</param>
  1154. /// <param name="recordCount">返回查询条数</param>
  1155. /// <returns></returns>
  1156. public DataTable FindTablePage<T>(string WhereSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount) where T : new()
  1157. {
  1158. StringBuilder strSql = DatabaseCommon.SelectSql<T>();
  1159. strSql.Append(WhereSql);
  1160. return SqlServerHelper.GetPageTable(strSql.ToString(), parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
  1161. }
  1162. /// <summary>
  1163. /// 查询数据列表、返回 DataTable
  1164. /// </summary>
  1165. /// <param name="strSql">Sql语句</param>
  1166. /// <param name="orderField">排序字段</param>
  1167. /// <param name="orderType">排序类型</param>
  1168. /// <param name="pageIndex">当前页</param>
  1169. /// <param name="pageSize">页大小</param>
  1170. /// <param name="recordCount">返回查询条数</param>
  1171. /// <returns></returns>
  1172. public DataTable FindTablePageBySql(string strSql, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
  1173. {
  1174. return SqlServerHelper.GetPageTable(strSql, orderField, orderType, pageIndex, pageSize, ref recordCount);
  1175. }
  1176. /// <summary>
  1177. /// 查询数据列表、返回 DataTable
  1178. /// </summary>
  1179. /// <param name="strSql">Sql语句</param>
  1180. /// <param name="parameters">sql语句对应参数</param>
  1181. /// <param name="orderField">排序字段</param>
  1182. /// <param name="orderType">排序类型</param>
  1183. /// <param name="pageIndex">当前页</param>
  1184. /// <param name="pageSize">页大小</param>
  1185. /// <param name="recordCount">返回查询条数</param>
  1186. /// <returns></returns>
  1187. public DataTable FindTablePageBySql(string strSql, DbParameter[] parameters, string orderField, string orderType, int pageIndex, int pageSize, ref int recordCount)
  1188. {
  1189. return SqlServerHelper.GetPageTable(strSql, parameters, orderField, orderType, pageIndex, pageSize, ref recordCount);
  1190. }
  1191. /// <summary>
  1192. /// 查询数据列表、返回 DataTable
  1193. /// </summary>
  1194. /// <param name="procName">存储过程</param>
  1195. /// <returns></returns>
  1196. public DataTable FindTableByProc(string procName)
  1197. {
  1198. IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, procName);
  1199. return DatabaseReader.ReaderToDataTable(dr);
  1200. }
  1201. /// <summary>
  1202. /// 查询数据列表、返回 DataTable
  1203. /// </summary>
  1204. /// <param name="procName">存储过程</param>
  1205. /// <param name="parameters">sql语句对应参数</param>
  1206. /// <returns></returns>
  1207. public DataTable FindTableByProc(string procName, DbParameter[] parameters)
  1208. {
  1209. IDataReader dr = DbHelper.ExecuteReader(CommandType.StoredProcedure, procName, parameters);
  1210. return DatabaseReader.ReaderToDataTable(dr);
  1211. }
  1212. #endregion
  1213. #region 查询数据列表、返回DataSet
  1214. /// <summary>
  1215. /// 查询数据列表、返回DataSet
  1216. /// </summary>
  1217. /// <param name="strSql">Sql语句</param>
  1218. /// <returns></returns>
  1219. public DataSet FindDataSetBySql(string strSql)
  1220. {
  1221. return DbHelper.GetDataSet(CommandType.Text, strSql);
  1222. }
  1223. /// <summary>
  1224. /// 查询数据列表、返回DataSet
  1225. /// </summary>
  1226. /// <param name="strSql">Sql语句</param>
  1227. /// <param name="parameters">sql语句对应参数</param>
  1228. /// <returns></returns>
  1229. public DataSet FindDataSetBySql(string strSql, DbParameter[] parameters)
  1230. {
  1231. return DbHelper.GetDataSet(CommandType.Text, strSql, parameters);
  1232. }
  1233. /// <summary>
  1234. /// 查询数据列表、返回DataSet
  1235. /// </summary>
  1236. /// <param name="strSql">存储过程</param>
  1237. /// <returns></returns>
  1238. public DataSet FindDataSetByProc(string procName)
  1239. {
  1240. return DbHelper.GetDataSet(CommandType.StoredProcedure, procName);
  1241. }
  1242. /// <summary>
  1243. /// 查询数据列表、返回DataSet
  1244. /// </summary>
  1245. /// <param name="strSql">存储过程</param>
  1246. /// <param name="parameters">sql语句对应参数</param>
  1247. /// <returns></returns>
  1248. public DataSet FindDataSetByProc(string procName, DbParameter[] parameters)
  1249. {
  1250. return DbHelper.GetDataSet(CommandType.StoredProcedure, procName, parameters);
  1251. }
  1252. #endregion
  1253. #region 查询对象、返回实体
  1254. /// <summary>
  1255. /// 查询对象、返回实体
  1256. /// </summary>
  1257. /// <param name="propertyValue">主键值</param>
  1258. /// <returns></returns>
  1259. public T FindEntity<T>(object propertyValue) where T : new()
  1260. {
  1261. string pkName = DatabaseCommon.GetKeyField<T>().ToString();//获取主键字段
  1262. StringBuilder strSql = DatabaseCommon.SelectSql<T>(1);
  1263. strSql.Append(" AND ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
  1264. IList<DbParameter> parameter = new List<DbParameter>();
  1265. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
  1266. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
  1267. return DatabaseReader.ReaderToModel<T>(dr);
  1268. }
  1269. /// <summary>
  1270. /// 查询对象、返回实体
  1271. /// </summary>
  1272. /// <param name="propertyName">实体属性名称</param>
  1273. /// <param name="propertyValue">字段值</param>
  1274. /// <returns></returns>
  1275. public T FindEntity<T>(string propertyName, object propertyValue) where T : new()
  1276. {
  1277. string pkName = propertyName;
  1278. StringBuilder strSql = DatabaseCommon.SelectSql<T>(1);
  1279. strSql.Append(" AND ").Append(pkName).Append("=").Append(DbHelper.DbParmChar + pkName);
  1280. IList<DbParameter> parameter = new List<DbParameter>();
  1281. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + pkName, propertyValue));
  1282. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
  1283. return DatabaseReader.ReaderToModel<T>(dr);
  1284. }
  1285. /// <summary>
  1286. /// 查询对象、返回实体
  1287. /// </summary>
  1288. /// <param name="WhereSql">条件</param>
  1289. /// <returns></returns>
  1290. public T FindEntityByWhere<T>(string WhereSql) where T : new()
  1291. {
  1292. StringBuilder strSql = DatabaseCommon.SelectSql<T>(1);
  1293. strSql.Append(WhereSql);
  1294. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1295. return DatabaseReader.ReaderToModel<T>(dr);
  1296. }
  1297. /// <summary>
  1298. /// 查询对象、返回实体
  1299. /// </summary>
  1300. /// <param name="WhereSql">条件</param>
  1301. /// <param name="parameters">sql语句对应参数</param>
  1302. /// <returns></returns>
  1303. public T FindEntityByWhere<T>(string WhereSql, DbParameter[] parameters) where T : new()
  1304. {
  1305. StringBuilder strSql = DatabaseCommon.SelectSql<T>(1);
  1306. strSql.Append(WhereSql);
  1307. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1308. return DatabaseReader.ReaderToModel<T>(dr);
  1309. }
  1310. /// <summary>
  1311. /// 查询对象、返回实体
  1312. /// </summary>
  1313. /// <param name="strSql">Sql语句</param>
  1314. /// <returns></returns>
  1315. public T FindEntityBySql<T>(string strSql)
  1316. {
  1317. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1318. return DatabaseReader.ReaderToModel<T>(dr);
  1319. }
  1320. /// <summary>
  1321. /// 查询对象、返回实体
  1322. /// </summary>
  1323. /// <param name="strSql">Sql语句</param>
  1324. /// <param name="parameters">sql语句对应参数</param>
  1325. /// <returns></returns>
  1326. public T FindEntityBySql<T>(string strSql, DbParameter[] parameters)
  1327. {
  1328. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1329. return DatabaseReader.ReaderToModel<T>(dr);
  1330. }
  1331. #endregion
  1332. #region 查询对象、返回哈希表
  1333. /// <summary>
  1334. /// 查询对象、返回哈希表
  1335. /// </summary>
  1336. /// <param name="tableName">表名</param>
  1337. /// <param name="propertyName">实体属性名称</param>
  1338. /// <param name="propertyValue">字段值</param>
  1339. /// <returns></returns>
  1340. public Hashtable FindHashtable(string tableName, string propertyName, object propertyValue)
  1341. {
  1342. StringBuilder strSql = DatabaseCommon.SelectSql(tableName, 1);
  1343. strSql.Append(" AND ").Append(propertyName).Append("=").Append(DbHelper.DbParmChar + propertyName);
  1344. IList<DbParameter> parameter = new List<DbParameter>();
  1345. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  1346. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameter.ToArray());
  1347. return DatabaseReader.ReaderToHashtable(dr);
  1348. }
  1349. /// <summary>
  1350. /// 查询对象、返回哈希表
  1351. /// </summary>
  1352. /// <param name="tableName">表名</param>
  1353. /// <param name="WhereSql">条件</param>
  1354. /// <returns></returns>
  1355. public Hashtable FindHashtable(string tableName, StringBuilder WhereSql)
  1356. {
  1357. StringBuilder strSql = DatabaseCommon.SelectSql(tableName, 1);
  1358. strSql.Append(WhereSql);
  1359. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1360. return DatabaseReader.ReaderToHashtable(dr);
  1361. }
  1362. /// <summary>
  1363. /// 查询对象、返回哈希表
  1364. /// </summary>
  1365. /// <param name="tableName">表名</param>
  1366. /// <param name="WhereSql">条件</param>
  1367. /// <param name="parameters">sql语句对应参数</param>
  1368. /// <returns></returns>
  1369. public Hashtable FindHashtable(string tableName, StringBuilder WhereSql, DbParameter[] parameters)
  1370. {
  1371. StringBuilder strSql = DatabaseCommon.SelectSql(tableName, 1);
  1372. strSql.Append(WhereSql);
  1373. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1374. return DatabaseReader.ReaderToHashtable(dr);
  1375. }
  1376. /// <summary>
  1377. /// 查询对象、返回哈希表
  1378. /// </summary>
  1379. /// <param name="strSql">Sql语句</param>
  1380. /// <returns></returns>
  1381. public Hashtable FindHashtableBySql(string strSql)
  1382. {
  1383. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString());
  1384. return DatabaseReader.ReaderToHashtable(dr);
  1385. }
  1386. /// <summary>
  1387. /// 查询对象、返回哈希表
  1388. /// </summary>
  1389. /// <param name="strSql">Sql语句</param>
  1390. /// <param name="parameters">sql语句对应参数</param>
  1391. /// <returns></returns>
  1392. public Hashtable FindHashtableBySql(string strSql, DbParameter[] parameters)
  1393. {
  1394. IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, strSql.ToString(), parameters);
  1395. return DatabaseReader.ReaderToHashtable(dr);
  1396. }
  1397. #endregion
  1398. #region 查询数据、返回条数
  1399. /// <summary>
  1400. /// 查询数据、返回条数
  1401. /// </summary>
  1402. /// <returns></returns>
  1403. public int FindCount<T>() where T : new()
  1404. {
  1405. StringBuilder strSql = DatabaseCommon.SelectCountSql<T>();
  1406. return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString()));
  1407. }
  1408. /// <summary>
  1409. /// 查询数据、返回条数
  1410. /// <param name="propertyName">实体属性名称</param>
  1411. /// <param name="propertyValue">字段值</param>
  1412. /// </summary>
  1413. /// <returns></returns>
  1414. public int FindCount<T>(string propertyName, string propertyValue) where T : new()
  1415. {
  1416. StringBuilder strSql = DatabaseCommon.SelectCountSql<T>();
  1417. strSql.Append(" AND " + propertyName + " = " + DbHelper.DbParmChar + propertyName);
  1418. IList<DbParameter> parameter = new List<DbParameter>();
  1419. parameter.Add(DbFactory.CreateDbParameter(DbHelper.DbParmChar + propertyName, propertyValue));
  1420. return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameter.ToArray()));
  1421. }
  1422. /// <summary>
  1423. /// 查询数据、返回条数
  1424. /// </summary>
  1425. /// <param name="WhereSql">条件</param>
  1426. /// <returns></returns>
  1427. public int FindCount<T>(string WhereSql) where T : new()
  1428. {
  1429. StringBuilder strSql = DatabaseCommon.SelectCountSql<T>();
  1430. strSql.Append(WhereSql);
  1431. return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString()));
  1432. }
  1433. /// <summary>
  1434. /// 查询数据、返回条数
  1435. /// </summary>
  1436. /// <param name="WhereSql">条件</param>
  1437. /// <param name="parameters">sql语句对应参数</param>
  1438. /// <returns></returns>
  1439. public int FindCount<T>(string WhereSql, DbParameter[] parameters) where T : new()
  1440. {
  1441. StringBuilder strSql = DatabaseCommon.SelectCountSql<T>();
  1442. strSql.Append(WhereSql);
  1443. return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters));
  1444. }
  1445. /// <summary>
  1446. /// 查询数据、返回条数
  1447. /// </summary>
  1448. /// <param name="strSql">Sql语句</param>
  1449. /// <returns></returns>
  1450. public int FindCountBySql(string strSql)
  1451. {
  1452. return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql));
  1453. }
  1454. /// <summary>
  1455. /// 查询数据、返回条数
  1456. /// </summary>
  1457. /// <param name="strSql">Sql语句</param>
  1458. /// <param name="parameters">sql语句对应参数</param>
  1459. /// <returns></returns>
  1460. public int FindCountBySql(string strSql, DbParameter[] parameters)
  1461. {
  1462. return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, strSql, parameters));
  1463. }
  1464. #endregion
  1465. #region 查询数据、返回最大数
  1466. /// <summary>
  1467. /// 查询数据、返回最大数
  1468. /// </summary>
  1469. /// <param name="propertyName">实体属性名称</param>
  1470. /// <returns></returns>
  1471. public object FindMax<T>(string propertyName) where T : new()
  1472. {
  1473. StringBuilder strSql = DatabaseCommon.SelectMaxSql<T>(propertyName);
  1474. return DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
  1475. }
  1476. /// <summary>
  1477. /// 查询数据、返回最大数
  1478. /// </summary>
  1479. /// <param name="propertyName">实体属性名称</param>
  1480. /// <param name="WhereSql">条件</param>
  1481. /// <returns></returns>
  1482. public object FindMax<T>(string propertyName, string WhereSql) where T : new()
  1483. {
  1484. StringBuilder strSql = DatabaseCommon.SelectMaxSql<T>(propertyName);
  1485. strSql.Append(WhereSql);
  1486. return DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString());
  1487. }
  1488. /// <summary>
  1489. /// 查询数据、返回最大数
  1490. /// </summary>
  1491. /// <param name="propertyName">实体属性名称</param>
  1492. /// <param name="WhereSql">条件</param>
  1493. /// <param name="parameters">sql语句对应参数</param>
  1494. /// <returns></returns>
  1495. public object FindMax<T>(string propertyName, string WhereSql, DbParameter[] parameters) where T : new()
  1496. {
  1497. StringBuilder strSql = DatabaseCommon.SelectMaxSql<T>(propertyName);
  1498. strSql.Append(WhereSql);
  1499. return DbHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
  1500. }
  1501. /// <summary>
  1502. /// 查询数据、返回最大数
  1503. /// </summary>
  1504. /// <param name="strSql">Sql语句</param>
  1505. /// <returns></returns>
  1506. public object FindMaxBySql(string strSql)
  1507. {
  1508. return DbHelper.ExecuteScalar(CommandType.Text, strSql);
  1509. }
  1510. /// <summary>
  1511. /// 查询数据、返回最大数
  1512. /// </summary>
  1513. /// <param name="strSql">Sql语句</param>
  1514. /// <param name="parameters">sql语句对应参数</param>
  1515. /// <returns></returns>
  1516. public object FindMaxBySql(string strSql, DbParameter[] parameters)
  1517. {
  1518. return DbHelper.ExecuteScalar(CommandType.Text, strSql, parameters);
  1519. }
  1520. #endregion
  1521. }
  1522. }