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.
 
 
 
 
 
 

1263 lines
41 KiB

  1. using Dapper;
  2. using Learun.Util;
  3. using Oracle.ManagedDataAccess.Client;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.Common;
  10. using System.Data.Entity;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Threading.Tasks;
  16. namespace Learun.DataBase.Oracle
  17. {
  18. /// <summary>
  19. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  20. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  21. /// 创建人:陈彬彬(Learun智慧校园数据库小组)
  22. /// 日 期:2017.03.04
  23. /// 描 述:数据库操作类
  24. /// </summary>
  25. public class Database : IDatabase
  26. {
  27. #region 构造函数
  28. /// <summary>
  29. /// 构造方法
  30. /// </summary>
  31. /// <param name="connString">连接串</param>
  32. public Database(string connString)
  33. {
  34. var obj = ConfigurationManager.ConnectionStrings[connString];
  35. string connectionString = obj == null ? connString : obj.ConnectionString;
  36. dbcontext = new DatabaseContext(connectionString);
  37. }
  38. #endregion
  39. #region 属性
  40. /// <summary>
  41. /// 获取 当前使用的数据访问上下文对象
  42. /// </summary>
  43. public DbContext dbcontext { get; set; }
  44. /// <summary>
  45. /// 事务对象
  46. /// </summary>
  47. public DbTransaction dbTransaction { get; set; }
  48. /// <summary>
  49. /// 获取连接上下文
  50. /// </summary>
  51. /// <returns></returns>
  52. public DbConnection getDbConnection()
  53. {
  54. return dbcontext.Database.Connection;
  55. }
  56. #endregion
  57. #region 事物提交
  58. /// <summary>
  59. /// 事务开始
  60. /// </summary>
  61. /// <returns></returns>
  62. public IDatabase BeginTrans()
  63. {
  64. if (dbcontext.Database.Connection.State == ConnectionState.Closed)
  65. {
  66. dbcontext.Database.Connection.Open();
  67. }
  68. dbTransaction = dbcontext.Database.Connection.BeginTransaction();
  69. dbcontext.Database.UseTransaction(dbTransaction);
  70. return this;
  71. }
  72. /// <summary>
  73. /// 提交当前操作的结果
  74. /// </summary>
  75. public int Commit()
  76. {
  77. try
  78. {
  79. int returnValue = dbcontext.SaveChanges();
  80. if (dbTransaction != null)
  81. {
  82. dbTransaction.Commit();
  83. this.Close();
  84. }
  85. return returnValue;
  86. }
  87. catch (Exception ex)
  88. {
  89. if (ex.InnerException != null && ex.InnerException.InnerException is OracleException)
  90. {
  91. OracleException sqlEx = ex.InnerException.InnerException as OracleException;
  92. throw ExceptionEx.ThrowDataAccessException(sqlEx, sqlEx.Message);
  93. }
  94. throw;
  95. }
  96. finally
  97. {
  98. if (dbTransaction == null)
  99. {
  100. this.Close();
  101. }
  102. }
  103. }
  104. public async Task<int> CommitAsync()
  105. {
  106. try
  107. {
  108. int returnValue =await dbcontext.SaveChangesAsync();
  109. if (dbTransaction != null)
  110. {
  111. dbTransaction.Commit();
  112. this.Close();
  113. }
  114. return returnValue;
  115. }
  116. catch (Exception ex)
  117. {
  118. if (ex.InnerException != null && ex.InnerException.InnerException is OracleException)
  119. {
  120. OracleException sqlEx = ex.InnerException.InnerException as OracleException;
  121. throw ExceptionEx.ThrowDataAccessException(sqlEx, sqlEx.Message);
  122. }
  123. throw;
  124. }
  125. finally
  126. {
  127. if (dbTransaction == null)
  128. {
  129. this.Close();
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// 把当前操作回滚成未提交状态
  135. /// </summary>
  136. public void Rollback()
  137. {
  138. this.dbTransaction.Rollback();
  139. this.dbTransaction.Dispose();
  140. this.Close();
  141. }
  142. /// <summary>
  143. /// 关闭连接 内存回收
  144. /// </summary>
  145. public void Close()
  146. {
  147. dbcontext.Dispose();
  148. }
  149. #endregion
  150. #region 执行 SQL 语句
  151. public async Task<int> ExecuteAsyncBySql(string strSql)
  152. {
  153. try
  154. {
  155. return await dbcontext.Database.Connection.ExecuteAsync(strSql, null, dbTransaction);
  156. }
  157. catch (Exception)
  158. {
  159. throw;
  160. }
  161. finally
  162. {
  163. if (dbTransaction == null)
  164. {
  165. this.Close();
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// 执行sql语句
  171. /// </summary>
  172. /// <param name="strSql">sql语句</param>
  173. /// <returns></returns>
  174. public int ExecuteBySql(string strSql)
  175. {
  176. try
  177. {
  178. return dbcontext.Database.Connection.Execute(strSql, null, dbTransaction);
  179. }
  180. catch (Exception)
  181. {
  182. throw;
  183. }
  184. finally
  185. {
  186. if (dbTransaction == null)
  187. {
  188. this.Close();
  189. }
  190. }
  191. }
  192. /// <summary>
  193. /// 执行sql语句(带参数)
  194. /// </summary>
  195. /// <param name="strSql">sql语句</param>
  196. /// <param name="dbParameter">参数</param>
  197. /// <returns></returns>
  198. public int ExecuteBySql(string strSql,object dbParameter)
  199. {
  200. try
  201. {
  202. strSql = strSql.Replace("@", ":");
  203. return dbcontext.Database.Connection.Execute(strSql, dbParameter, dbTransaction);
  204. }
  205. catch (Exception)
  206. {
  207. throw;
  208. }
  209. finally
  210. {
  211. if (dbTransaction == null)
  212. {
  213. this.Close();
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// 执行存储过程
  219. /// </summary>
  220. /// <param name="procName">存储过程名称</param>
  221. /// <returns></returns>
  222. public int ExecuteByProc(string procName)
  223. {
  224. try
  225. {
  226. return dbcontext.Database.Connection.Execute(procName, null, dbTransaction, null, CommandType.StoredProcedure);
  227. }
  228. catch (Exception)
  229. {
  230. throw;
  231. }
  232. finally
  233. {
  234. if (dbTransaction == null)
  235. {
  236. this.Close();
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// 执行存储过程
  242. /// </summary>
  243. /// <param name="procName">存储过程名称</param>
  244. /// <param name="dbParameter">参数</param>
  245. /// <returns></returns>
  246. public int ExecuteByProc(string procName, object dbParameter)
  247. {
  248. try
  249. {
  250. return dbcontext.Database.Connection.Execute(procName, dbParameter, dbTransaction, null, CommandType.StoredProcedure);
  251. }
  252. catch (Exception)
  253. {
  254. throw;
  255. }
  256. finally
  257. {
  258. if (dbTransaction == null)
  259. {
  260. this.Close();
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// 执行存储过程
  266. /// </summary>
  267. /// <param name="procName">存储过程名称</param>
  268. /// <returns></returns>
  269. public T ExecuteByProc<T>(string procName) where T : class
  270. {
  271. try
  272. {
  273. return dbcontext.Database.Connection.ExecuteScalar<T>(procName, null, dbTransaction, null, CommandType.StoredProcedure);
  274. }
  275. catch (Exception)
  276. {
  277. throw;
  278. }
  279. finally
  280. {
  281. if (dbTransaction == null)
  282. {
  283. this.Close();
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// 执行存储过程
  289. /// </summary>
  290. /// <param name="procName">存储过程名称</param>
  291. /// <param name="dbParameter">参数</param>
  292. /// <returns></returns>
  293. public T ExecuteByProc<T>(string procName, object dbParameter) where T : class
  294. {
  295. try
  296. {
  297. return dbcontext.Database.Connection.ExecuteScalar<T>(procName, dbParameter, dbTransaction, null, CommandType.StoredProcedure);
  298. }
  299. catch (Exception)
  300. {
  301. throw;
  302. }
  303. finally
  304. {
  305. if (dbTransaction == null)
  306. {
  307. this.Close();
  308. }
  309. }
  310. }
  311. /// <summary>
  312. /// 执行存储过程
  313. /// </summary>
  314. /// <param name="procName">存储过程名称</param>
  315. /// <returns></returns>
  316. public IEnumerable<T> QueryByProc<T>(string procName) where T : class
  317. {
  318. try
  319. {
  320. return dbcontext.Database.Connection.Query<T>(procName, null, dbTransaction, true, null, CommandType.StoredProcedure);
  321. }
  322. catch (Exception)
  323. {
  324. throw;
  325. }
  326. finally
  327. {
  328. if (dbTransaction == null)
  329. {
  330. this.Close();
  331. }
  332. }
  333. }
  334. /// <summary>
  335. /// 执行存储过程
  336. /// </summary>
  337. /// <param name="procName">存储过程名称</param>
  338. /// <param name="dbParameter">参数</param>
  339. /// <returns></returns>
  340. public IEnumerable<T> QueryByProc<T>(string procName, object dbParameter) where T : class
  341. {
  342. try
  343. {
  344. return dbcontext.Database.Connection.Query<T>(procName, dbParameter, dbTransaction, true, null, CommandType.StoredProcedure);
  345. }
  346. catch (Exception)
  347. {
  348. throw;
  349. }
  350. finally
  351. {
  352. if (dbTransaction == null)
  353. {
  354. this.Close();
  355. }
  356. }
  357. }
  358. /// <summary>
  359. /// 执行存储过程(获取集)
  360. /// </summary>
  361. /// <param name="procName">存储过程名称</param>
  362. /// <param name="dbParameter">参数</param>
  363. /// <returns></returns>
  364. public DataTable FindTableByProc(string procName, object dbParameter)
  365. {
  366. try
  367. {
  368. var IDataReader = dbcontext.Database.Connection.ExecuteReader(procName, dbParameter, dbTransaction, null, CommandType.StoredProcedure);
  369. return SqlHelper.IDataReaderToDataTable(IDataReader);
  370. }
  371. catch (Exception)
  372. {
  373. throw;
  374. }
  375. finally
  376. {
  377. if (dbTransaction == null)
  378. {
  379. this.Close();
  380. }
  381. }
  382. }
  383. #endregion
  384. #region 对象实体 添加、修改、删除
  385. /// <summary>
  386. /// 插入实体数据
  387. /// </summary>
  388. /// <typeparam name="T">类型</typeparam>
  389. /// <param name="entity">实体数据</param>
  390. /// <returns></returns>
  391. public int Insert<T>(T entity) where T : class
  392. {
  393. dbcontext.Entry<T>(entity).State = EntityState.Added;
  394. return dbTransaction == null ? this.Commit() : 0;
  395. }
  396. /// <summary>
  397. /// 批量插入实体数据
  398. /// </summary>
  399. /// <typeparam name="T">类型</typeparam>
  400. /// <param name="entities">实体数据列表</param>
  401. /// <returns></returns>
  402. public int Insert<T>(IEnumerable<T> entities) where T : class
  403. {
  404. foreach (var entity in entities)
  405. {
  406. dbcontext.Entry<T>(entity).State = EntityState.Added;
  407. }
  408. return dbTransaction == null ? this.Commit() : 0;
  409. }
  410. public async Task<int> InsertAsync<T>(IEnumerable<T> entities) where T : class
  411. {
  412. foreach (var entity in entities)
  413. {
  414. dbcontext.Entry<T>(entity).State = EntityState.Added;
  415. }
  416. return dbTransaction == null ?await this.CommitAsync() : 0;
  417. }
  418. /// <summary>
  419. /// 删除实体数据
  420. /// </summary>
  421. /// <typeparam name="T">类型</typeparam>
  422. /// <param name="entity">实体数据(需要主键赋值)</param>
  423. /// <returns></returns>
  424. public int Delete<T>(T entity) where T : class
  425. {
  426. dbcontext.Set<T>().Attach(entity);
  427. dbcontext.Set<T>().Remove(entity);
  428. return dbTransaction == null ? this.Commit() : 0;
  429. }
  430. /// <summary>
  431. /// 批量删除实体数据
  432. /// </summary>
  433. /// <typeparam name="T">类型</typeparam>
  434. /// <param name="entities">实体数据列表</param>
  435. /// <returns></returns>
  436. public int Delete<T>(IEnumerable<T> entities) where T : class
  437. {
  438. foreach (var entity in entities)
  439. {
  440. dbcontext.Set<T>().Attach(entity);
  441. dbcontext.Set<T>().Remove(entity);
  442. }
  443. return dbTransaction == null ? this.Commit() : 0;
  444. }
  445. /// <summary>
  446. /// 删除表数据(根据Lambda表达式)
  447. /// </summary>
  448. /// <typeparam name="T"></typeparam>
  449. /// <param name="condition"></param>
  450. /// <returns></returns>
  451. public int Delete<T>(Expression<Func<T, bool>> condition) where T : class,new()
  452. {
  453. IEnumerable<T> entities = dbcontext.Set<T>().Where(condition).ToList();
  454. return entities.Count() > 0 ? Delete(entities) : 0;
  455. }
  456. /// <summary>
  457. /// 更新实体数据
  458. /// </summary>
  459. /// <typeparam name="T">类型</typeparam>
  460. /// <param name="entity">实体数据</param>
  461. /// <returns></returns>
  462. public int Update<T>(T entity) where T : class
  463. {
  464. this.UpdateEntity(entity);
  465. return dbTransaction == null ? this.Commit() : 0;
  466. }
  467. /// <summary>
  468. /// 更新实体数据
  469. /// </summary>
  470. /// <typeparam name="T">类型</typeparam>
  471. /// <param name="entity">实体数据</param>
  472. /// <returns></returns>
  473. public int UpdateEx<T>(T entity) where T : class
  474. {
  475. dbcontext.Set<T>().Attach(entity);
  476. dbcontext.Entry(entity).State = EntityState.Modified;
  477. return dbTransaction == null ? this.Commit() : 0;
  478. }
  479. /// <summary>
  480. /// 批量更新实体数据
  481. /// </summary>
  482. /// <typeparam name="T">类型</typeparam>
  483. /// <param name="entities">实体数据列表</param>
  484. /// <returns></returns>
  485. public int Update<T>(IEnumerable<T> entities) where T : class
  486. {
  487. foreach (var entity in entities)
  488. {
  489. this.UpdateEntity(entity);
  490. }
  491. return dbTransaction == null ? this.Commit() : 0;
  492. }
  493. /// <summary>
  494. /// EF更新实体
  495. /// </summary>
  496. /// <typeparam name="T">类型</typeparam>
  497. /// <param name="entity">实体数据</param>
  498. private void UpdateEntity<T>(T entity) where T : class
  499. {
  500. dbcontext.Set<T>().Attach(entity);
  501. Hashtable props = SqlHelper.GetPropertyInfo<T>(entity);
  502. foreach (string item in props.Keys)
  503. {
  504. object value = dbcontext.Entry(entity).Property(item).CurrentValue;
  505. if (value != null)
  506. {
  507. if (value.ToString() == "&nbsp;")
  508. dbcontext.Entry(entity).Property(item).CurrentValue = null;
  509. dbcontext.Entry(entity).Property(item).IsModified = true;
  510. }
  511. }
  512. }
  513. #endregion
  514. #region 对象实体 查询
  515. public async Task<IEnumerable<T>> FindListAsync<T>(string strSql) where T : class
  516. {
  517. try
  518. {
  519. strSql = strSql.Replace("@", "?");
  520. return await dbcontext.Database.Connection.QueryAsync<T>(strSql, null, dbTransaction);
  521. }
  522. catch (Exception)
  523. {
  524. throw;
  525. }
  526. finally
  527. {
  528. if (dbTransaction == null)
  529. {
  530. this.Close();
  531. }
  532. }
  533. }
  534. /// <summary>
  535. /// 查找一个实体根据主键
  536. /// </summary>
  537. /// <typeparam name="T">类型</typeparam>
  538. /// <param name="KeyValue">主键</param>
  539. /// <returns></returns>
  540. public T FindEntity<T>(object keyValue) where T : class
  541. {
  542. try
  543. {
  544. return dbcontext.Set<T>().Find(keyValue);
  545. }
  546. catch (Exception)
  547. {
  548. throw;
  549. }
  550. finally
  551. {
  552. if (dbTransaction == null)
  553. {
  554. this.Close();
  555. }
  556. }
  557. }
  558. /// <summary>
  559. /// 查找一个实体(根据表达式)
  560. /// </summary>
  561. /// <typeparam name="T">类型</typeparam>
  562. /// <param name="condition">表达式</param>
  563. /// <returns></returns>
  564. public T FindEntity<T>(Expression<Func<T, bool>> condition) where T : class,new()
  565. {
  566. try
  567. {
  568. return dbcontext.Set<T>().Where(condition).FirstOrDefault();
  569. }
  570. catch (Exception)
  571. {
  572. throw;
  573. }
  574. finally
  575. {
  576. if (dbTransaction == null)
  577. {
  578. this.Close();
  579. }
  580. }
  581. }
  582. public async Task<IEnumerable<T>> FindListAsync<T>(string strSql, object dbParameter) where T : class
  583. {
  584. try
  585. {
  586. strSql = strSql.Replace("@", "?");
  587. return await dbcontext.Database.Connection.QueryAsync<T>(strSql, dbParameter, dbTransaction);
  588. }
  589. catch (Exception)
  590. {
  591. throw;
  592. }
  593. finally
  594. {
  595. if (dbTransaction == null)
  596. {
  597. this.Close();
  598. }
  599. }
  600. }
  601. /// <summary>
  602. /// 查找一个实体(根据sql)
  603. /// </summary>
  604. /// <typeparam name="T">类型</typeparam>
  605. /// <param name="strSql">sql语句</param>
  606. /// <param name="dbParameter">参数</param>
  607. /// <returns></returns>
  608. public T FindEntity<T>(string strSql, object dbParameter = null) where T : class,new()
  609. {
  610. try
  611. {
  612. strSql = strSql.Replace("@", ":");
  613. var data = dbcontext.Database.Connection.Query<T>(strSql, dbParameter, dbTransaction);
  614. return data.FirstOrDefault();
  615. }
  616. catch (Exception)
  617. {
  618. throw;
  619. }
  620. finally
  621. {
  622. if (dbTransaction == null)
  623. {
  624. this.Close();
  625. }
  626. }
  627. }
  628. /// <summary>
  629. /// 获取IQueryable表达式
  630. /// </summary>
  631. /// <typeparam name="T">类型</typeparam>
  632. /// <returns></returns>
  633. public IQueryable<T> IQueryable<T>() where T : class,new()
  634. {
  635. try
  636. {
  637. return dbcontext.Set<T>();
  638. }
  639. catch (Exception)
  640. {
  641. throw;
  642. }
  643. finally
  644. {
  645. if (dbTransaction == null)
  646. {
  647. this.Close();
  648. }
  649. }
  650. }
  651. /// <summary>
  652. /// 获取IQueryable表达式(根据表达式)
  653. /// </summary>
  654. /// <typeparam name="T">类型</typeparam>
  655. /// <param name="condition">表达式</param>
  656. /// <returns></returns>
  657. public IQueryable<T> IQueryable<T>(Expression<Func<T, bool>> condition) where T : class,new()
  658. {
  659. try
  660. {
  661. return dbcontext.Set<T>().Where(condition);
  662. }
  663. catch (Exception)
  664. {
  665. throw;
  666. }
  667. finally
  668. {
  669. if (dbTransaction == null)
  670. {
  671. this.Close();
  672. }
  673. }
  674. }
  675. /// <summary>
  676. /// 查询列表(获取表所有数据)
  677. /// </summary>
  678. /// <typeparam name="T">类型</typeparam>
  679. /// <returns></returns>
  680. public IEnumerable<T> FindList<T>() where T : class,new()
  681. {
  682. try
  683. {
  684. return dbcontext.Set<T>().ToList();
  685. }
  686. catch (Exception)
  687. {
  688. throw;
  689. }
  690. finally
  691. {
  692. if (dbTransaction == null)
  693. {
  694. this.Close();
  695. }
  696. }
  697. }
  698. public async Task<IEnumerable<T>> FindListAsync<T>() where T : class, new()
  699. {
  700. try
  701. {
  702. return await dbcontext.Set<T>().ToListAsync();
  703. }
  704. catch (Exception)
  705. {
  706. throw;
  707. }
  708. finally
  709. {
  710. if (dbTransaction == null)
  711. {
  712. this.Close();
  713. }
  714. }
  715. }
  716. /// <summary>
  717. /// 查询列表(获取表所有数据)
  718. /// </summary>
  719. /// <typeparam name="T">类型</typeparam>
  720. /// <param name="orderby">排序</param>
  721. /// <returns></returns>
  722. public IEnumerable<T> FindList<T>(Func<T, object> keySelector) where T : class,new()
  723. {
  724. try
  725. {
  726. return dbcontext.Set<T>().OrderBy(keySelector).ToList();
  727. }
  728. catch (Exception)
  729. {
  730. throw;
  731. }
  732. finally
  733. {
  734. if (dbTransaction == null)
  735. {
  736. this.Close();
  737. }
  738. }
  739. }
  740. /// <summary>
  741. /// 查询列表根据表达式
  742. /// </summary>
  743. /// <typeparam name="T">类型</typeparam>
  744. /// <param name="condition">表达式</param>
  745. /// <returns></returns>
  746. public IEnumerable<T> FindList<T>(Expression<Func<T, bool>> condition) where T : class,new()
  747. {
  748. try
  749. {
  750. return dbcontext.Set<T>().Where(condition).ToList();
  751. }
  752. catch (Exception)
  753. {
  754. throw;
  755. }
  756. finally
  757. {
  758. if (dbTransaction == null)
  759. {
  760. this.Close();
  761. }
  762. }
  763. }
  764. /// <summary>
  765. /// 查询列表根据sql语句
  766. /// </summary>
  767. /// <typeparam name="T">类型</typeparam>
  768. /// <param name="strSql">sql语句</param>
  769. /// <returns></returns>
  770. public IEnumerable<T> FindList<T>(string strSql) where T : class
  771. {
  772. try
  773. {
  774. strSql = strSql.Replace("@", ":");
  775. return dbcontext.Database.Connection.Query<T>(strSql, null, dbTransaction);
  776. }
  777. catch (Exception)
  778. {
  779. throw;
  780. }
  781. finally
  782. {
  783. if (dbTransaction == null)
  784. {
  785. this.Close();
  786. }
  787. }
  788. }
  789. /// <summary>
  790. /// 查询列表根据sql语句(带参数)
  791. /// </summary>
  792. /// <typeparam name="T">类型</typeparam>
  793. /// <param name="strSql">sql语句</param>
  794. /// <param name="dbParameter">参数</param>
  795. /// <returns></returns>
  796. public IEnumerable<T> FindList<T>(string strSql, object dbParameter) where T : class
  797. {
  798. try
  799. {
  800. strSql = strSql.Replace("@", ":");
  801. return dbcontext.Database.Connection.Query<T>(strSql, dbParameter, dbTransaction);
  802. }
  803. catch (Exception)
  804. {
  805. throw;
  806. }
  807. finally
  808. {
  809. if (dbTransaction == null)
  810. {
  811. this.Close();
  812. }
  813. }
  814. }
  815. /// <summary>
  816. /// 查询列表(分页)
  817. /// </summary>
  818. /// <typeparam name="T">类型</typeparam>
  819. /// <param name="orderField">排序字段</param>
  820. /// <param name="isAsc">排序类型</param>
  821. /// <param name="pageSize">每页数据条数</param>
  822. /// <param name="pageIndex">页码</param>
  823. /// <param name="total">总共数据条数</param>
  824. /// <returns></returns>
  825. public IEnumerable<T> FindList<T>(string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class,new()
  826. {
  827. try
  828. {
  829. string[] _order = orderField.Split(',');
  830. MethodCallExpression resultExp = null;
  831. var tempData = dbcontext.Set<T>().AsQueryable();
  832. foreach (string item in _order)
  833. {
  834. string _orderPart = item;
  835. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  836. string[] _orderArry = _orderPart.Split(' ');
  837. string _orderField = _orderArry[0];
  838. bool sort = isAsc;
  839. if (_orderArry.Length == 2)
  840. {
  841. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  842. }
  843. var parameter = Expression.Parameter(typeof(T), "t");
  844. var property = typeof(T).GetProperty(_orderField);
  845. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  846. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  847. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  848. }
  849. tempData = tempData.Provider.CreateQuery<T>(resultExp);
  850. total = tempData.Count();
  851. tempData = tempData.Skip<T>(pageSize * (pageIndex - 1)).Take<T>(pageSize).AsQueryable();
  852. return tempData.ToList();
  853. }
  854. catch (Exception)
  855. {
  856. throw;
  857. }
  858. finally
  859. {
  860. if (dbTransaction == null)
  861. {
  862. this.Close();
  863. }
  864. }
  865. }
  866. /// <summary>
  867. /// 查询列表(分页)带表达式条件
  868. /// </summary>
  869. /// <typeparam name="T">类型</typeparam>
  870. /// <param name="condition">表达式</param>
  871. /// <param name="orderField">排序字段</param>
  872. /// <param name="isAsc">排序类型</param>
  873. /// <param name="pageSize">每页数据条数</param>
  874. /// <param name="pageIndex">页码</param>
  875. /// <param name="total">总共数据条数</param>
  876. /// <returns></returns>
  877. public IEnumerable<T> FindList<T>(Expression<Func<T, bool>> condition, string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class,new()
  878. {
  879. try
  880. {
  881. string[] _order = orderField.Split(',');
  882. MethodCallExpression resultExp = null;
  883. var tempData = dbcontext.Set<T>().Where(condition);
  884. foreach (string item in _order)
  885. {
  886. string _orderPart = item;
  887. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  888. string[] _orderArry = _orderPart.Split(' ');
  889. string _orderField = _orderArry[0];
  890. bool sort = isAsc;
  891. if (_orderArry.Length == 2)
  892. {
  893. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  894. }
  895. var parameter = Expression.Parameter(typeof(T), "t");
  896. var property = typeof(T).GetProperty(_orderField);
  897. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  898. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  899. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(T), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  900. }
  901. tempData = tempData.Provider.CreateQuery<T>(resultExp);
  902. total = tempData.Count();
  903. tempData = tempData.Skip<T>(pageSize * (pageIndex - 1)).Take<T>(pageSize).AsQueryable();
  904. return tempData.ToList();
  905. }
  906. catch (Exception)
  907. {
  908. throw;
  909. }
  910. finally
  911. {
  912. if (dbTransaction == null)
  913. {
  914. this.Close();
  915. }
  916. }
  917. }
  918. /// <summary>
  919. /// 查询列表(分页)根据sql语句
  920. /// </summary>
  921. /// <typeparam name="T"></typeparam>
  922. /// <param name="strSql">sql语句</param>
  923. /// <param name="orderField">排序字段</param>
  924. /// <param name="isAsc">排序类型</param>
  925. /// <param name="pageSize">每页数据条数</param>
  926. /// <param name="pageIndex">页码</param>
  927. /// <param name="total">总共数据条数</param>
  928. /// <returns></returns>
  929. public IEnumerable<T> FindList<T>(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class
  930. {
  931. return FindList<T>(strSql, null, orderField, isAsc, pageSize, pageIndex, out total);
  932. }
  933. /// <summary>
  934. /// 查询列表(分页)根据sql语句
  935. /// </summary>
  936. /// <typeparam name="T"></typeparam>
  937. /// <param name="strSql">sql语句</param>
  938. /// <param name="dbParameter">参数</param>
  939. /// <param name="orderField">排序字段</param>
  940. /// <param name="isAsc">排序类型</param>
  941. /// <param name="pageSize">每页数据条数</param>
  942. /// <param name="pageIndex">页码</param>
  943. /// <param name="total">总共数据条数</param>
  944. /// <returns></returns>
  945. public IEnumerable<T> FindList<T>(string strSql, object dbParameter, string orderField, bool isAsc, int pageSize, int pageIndex, out int total) where T : class
  946. {
  947. try
  948. {
  949. strSql = strSql.Replace("@", ":");
  950. StringBuilder sb = new StringBuilder();
  951. sb.Append(SqlHelper.OraclePageSql(strSql, orderField, isAsc, pageSize, pageIndex));
  952. total = Convert.ToInt32(dbcontext.Database.Connection.ExecuteScalar("Select Count(1) From (" + strSql + ") t", dbParameter));
  953. return dbcontext.Database.Connection.Query<T>(sb.ToString(), dbParameter, dbTransaction);
  954. }
  955. catch (Exception)
  956. {
  957. throw;
  958. }
  959. finally
  960. {
  961. if (dbTransaction == null)
  962. {
  963. this.Close();
  964. }
  965. }
  966. }
  967. #endregion
  968. #region 数据源查询
  969. /// <summary>
  970. /// 查询数据
  971. /// </summary>
  972. /// <param name="strSql">sql语句</param>
  973. /// <returns></returns>
  974. public DataTable FindTable(string strSql)
  975. {
  976. try
  977. {
  978. var IDataReader = dbcontext.Database.Connection.ExecuteReader(strSql, null, dbTransaction);
  979. return SqlHelper.IDataReaderToDataTable(IDataReader);
  980. }
  981. catch (Exception)
  982. {
  983. throw;
  984. }
  985. finally
  986. {
  987. if (dbTransaction == null)
  988. {
  989. this.Close();
  990. }
  991. }
  992. }
  993. /// <summary>
  994. /// 查询数据
  995. /// </summary>
  996. /// <param name="strSql">sql语句</param>
  997. /// <param name="dbParameter">参数</param>
  998. /// <returns></returns>
  999. public DataTable FindTable(string strSql,object dbParameter)
  1000. {
  1001. try
  1002. {
  1003. strSql = strSql.Replace("@", ":");
  1004. var IDataReader = dbcontext.Database.Connection.ExecuteReader(strSql, dbParameter, dbTransaction);
  1005. return SqlHelper.IDataReaderToDataTable(IDataReader);
  1006. }
  1007. catch (Exception)
  1008. {
  1009. throw;
  1010. }
  1011. finally
  1012. {
  1013. if (dbTransaction == null)
  1014. {
  1015. this.Close();
  1016. }
  1017. }
  1018. }
  1019. /// <summary>
  1020. /// 查询数据
  1021. /// </summary>
  1022. /// <param name="strSql">sql语句</param>
  1023. /// <param name="orderField">排序字段</param>
  1024. /// <param name="isAsc">排序类型</param>
  1025. /// <param name="pageSize">每页数据条数</param>
  1026. /// <param name="pageIndex">页码</param>
  1027. /// <param name="total">总共数据条数</param>
  1028. /// <returns></returns>
  1029. public DataTable FindTable(string strSql, string orderField, bool isAsc, int pageSize, int pageIndex, out int total)
  1030. {
  1031. return FindTable(strSql, null, orderField, isAsc, pageSize, pageIndex, out total);
  1032. }
  1033. /// <summary>
  1034. /// 查询数据
  1035. /// </summary>
  1036. /// <param name="strSql">sql语句</param>
  1037. /// <param name="dbParameter">参数</param>
  1038. /// <param name="orderField">排序字段</param>
  1039. /// <param name="isAsc">排序类型</param>
  1040. /// <param name="pageSize">每页数据条数</param>
  1041. /// <param name="pageIndex">页码</param>
  1042. /// <param name="total">总共数据条数</param>
  1043. /// <returns></returns>
  1044. public DataTable FindTable(string strSql, object dbParameter, string orderField, bool isAsc, int pageSize, int pageIndex, out int total)
  1045. {
  1046. try
  1047. {
  1048. strSql = strSql.Replace("@", ":");
  1049. StringBuilder sb = new StringBuilder();
  1050. sb.Append(SqlHelper.OraclePageSql(strSql, orderField, isAsc, pageSize, pageIndex));
  1051. total = Convert.ToInt32(dbcontext.Database.Connection.ExecuteScalar("Select Count(1) From (" + strSql + ") t", dbParameter));
  1052. var IDataReader = dbcontext.Database.Connection.ExecuteReader(sb.ToString(), dbParameter, dbTransaction);
  1053. return SqlHelper.IDataReaderToDataTable(IDataReader);
  1054. }
  1055. catch (Exception)
  1056. {
  1057. throw;
  1058. }
  1059. finally
  1060. {
  1061. if (dbTransaction == null)
  1062. {
  1063. this.Close();
  1064. }
  1065. }
  1066. }
  1067. /// <summary>
  1068. /// 获取查询对象
  1069. /// </summary>
  1070. /// <param name="strSql">sql语句</param>
  1071. /// <returns></returns>
  1072. public object FindObject(string strSql)
  1073. {
  1074. return FindObject(strSql, null);
  1075. }
  1076. /// <summary>
  1077. /// 获取查询对象
  1078. /// </summary>
  1079. /// <param name="strSql">sql语句</param>
  1080. /// <param name="dbParameter">参数</param>
  1081. /// <returns></returns>
  1082. public object FindObject(string strSql,object dbParameter)
  1083. {
  1084. try
  1085. {
  1086. strSql = strSql.Replace("@", ":");
  1087. return dbcontext.Database.Connection.ExecuteScalar(strSql, dbParameter, dbTransaction);
  1088. }
  1089. catch (Exception)
  1090. {
  1091. throw;
  1092. }
  1093. finally
  1094. {
  1095. if (dbTransaction == null)
  1096. {
  1097. this.Close();
  1098. }
  1099. }
  1100. }
  1101. #endregion
  1102. #region 扩展方法
  1103. /// <summary>
  1104. /// 获取数据库表数据
  1105. /// </summary>
  1106. /// <typeparam name="T">反序列化类型</typeparam>
  1107. /// <returns></returns>
  1108. public IEnumerable<T> GetDBTable<T>() where T : class,new()
  1109. {
  1110. try
  1111. {
  1112. StringBuilder strSql = new StringBuilder();
  1113. strSql.Append(@"
  1114. select distinct col.table_name name,
  1115. 0 reserved,
  1116. 0 fdata,
  1117. 0 index_size,
  1118. nvl(t.num_rows, 0) sumrows,
  1119. 0 funused,
  1120. tab.comments tdescription,
  1121. column_name pk
  1122. from user_cons_columns col
  1123. inner join user_constraints con
  1124. on con.constraint_name = col.constraint_name
  1125. inner join user_tab_comments tab
  1126. on tab.table_name = col.table_name
  1127. inner join user_tables t
  1128. on t.TABLE_NAME = col.table_name
  1129. where con.constraint_type not in ('C', 'R') ORDER BY col.table_name
  1130. ");
  1131. return dbcontext.Database.Connection.Query<T>(strSql.ToString(), null, dbTransaction);
  1132. }
  1133. catch (Exception)
  1134. {
  1135. throw;
  1136. }
  1137. finally
  1138. {
  1139. if (dbTransaction == null)
  1140. {
  1141. this.Close();
  1142. }
  1143. }
  1144. }
  1145. /// <summary>
  1146. /// 获取数据库表字段数据
  1147. /// </summary>
  1148. /// <typeparam name="T">反序列化类型</typeparam>
  1149. /// <param name="tableName">表名</param>
  1150. /// <returns></returns>
  1151. public IEnumerable<T> GetDBTableFields<T>(string tableName) where T : class,new()
  1152. {
  1153. try
  1154. {
  1155. StringBuilder strSql = new StringBuilder();
  1156. strSql.Append(@"SELECT
  1157. col.column_id f_number,
  1158. col.column_name f_column,
  1159. col.data_type f_datatype,
  1160. col.data_length f_length,
  1161. NULL f_identity,
  1162. CASE uc.constraint_type
  1163. WHEN 'P' THEN
  1164. 1
  1165. ELSE
  1166. NULL
  1167. END f_key,
  1168. CASE col.nullable
  1169. WHEN 'N' THEN
  1170. 0
  1171. ELSE
  1172. 1
  1173. END f_isnullable,
  1174. col.data_default f_defaults,
  1175. NVL(comm.comments, col.column_name) AS f_remark
  1176. FROM
  1177. user_tab_columns col
  1178. INNER JOIN user_col_comments comm ON comm.TABLE_NAME = col.TABLE_NAME
  1179. AND comm.COLUMN_NAME = col.COLUMN_NAME
  1180. LEFT JOIN user_cons_columns ucc ON ucc.table_name = col.table_name
  1181. AND ucc.column_name = col.column_name
  1182. AND ucc.position = 1
  1183. LEFT JOIN user_constraints uc ON uc.constraint_name = ucc.constraint_name
  1184. AND uc.constraint_type = 'P'
  1185. WHERE
  1186. col.table_name = :tableName
  1187. ORDER BY
  1188. col.column_id");
  1189. return dbcontext.Database.Connection.Query<T>(strSql.ToString(), new { tableName = tableName.ToUpper() }, dbTransaction);
  1190. }
  1191. catch (Exception)
  1192. {
  1193. throw;
  1194. }
  1195. finally
  1196. {
  1197. if (dbTransaction == null)
  1198. {
  1199. this.Close();
  1200. }
  1201. }
  1202. }
  1203. #endregion
  1204. }
  1205. }