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.

1592 lines
69 KiB

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