MsSqlDatabase.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using CP.Model.ZiXun.Query;
  11. using Dapper;
  12. using DapperLambda;
  13. namespace CP.Dapper
  14. {
  15. public class MsSqlDatabase
  16. {
  17. public MsSqlDatabase(string connString)
  18. {
  19. ConnectionString = connString;
  20. }
  21. #region 属性
  22. /// <summary>
  23. /// 数据库连接字符串
  24. /// </summary>
  25. private string ConnectionString { get; set; }
  26. /// <summary>
  27. /// 获取 数据库连接串
  28. /// </summary>
  29. private DbConnection Connection
  30. {
  31. get
  32. {
  33. DbConnection dbconnection = new SqlConnection(ConnectionString);
  34. if (dbconnection.State == ConnectionState.Closed)
  35. {
  36. dbconnection.Open();
  37. }
  38. return dbconnection;
  39. }
  40. }
  41. /// <summary>
  42. /// 获取DapperLambda数据连接
  43. /// </summary>
  44. private DbContext Context
  45. {
  46. get
  47. {
  48. return new DbContext().ConnectionString(ConnectionString, DatabaseType.MSSQLServer);
  49. }
  50. }
  51. #endregion 属性
  52. /// <summary>
  53. /// 集合查询
  54. /// </summary>
  55. /// <typeparam name="T"></typeparam>
  56. /// <param name="sql"></param>
  57. /// <param name="para"></param>
  58. /// <returns></returns>
  59. public IEnumerable<T> FindList<T>(string sql, object para = null)
  60. where T : class, new()
  61. {
  62. using (var db = Connection)
  63. {
  64. return db.Query<T>(sql, para).ToList();
  65. }
  66. }
  67. /// <summary>
  68. /// 分页集合查询(Lambda)
  69. /// </summary>
  70. /// <typeparam name="T"></typeparam>
  71. /// <param name="condition"></param>
  72. /// <returns></returns>
  73. public IEnumerable<T> FindPageList<T>(string sql, PaginationQuery pagination, out int total, object para = null) where T : class, new()
  74. {
  75. using (var db = Connection)
  76. {
  77. var orderBy = "";
  78. if (!string.IsNullOrEmpty(pagination.Sidx))
  79. {
  80. if (pagination.Sord.ToUpper().IndexOf("ASC", StringComparison.Ordinal) + pagination.Sord.ToUpper().IndexOf("DESC", StringComparison.Ordinal) > 0)
  81. {
  82. orderBy = "Order By " + pagination.Sord;
  83. }
  84. else
  85. {
  86. orderBy = "Order By " + pagination.Sidx + " " + pagination.Sord.ToUpper();
  87. }
  88. }
  89. else
  90. {
  91. orderBy = "Order By (Select 0)";
  92. }
  93. var sqls = string.Format(@"
  94. Select * From (Select ROW_NUMBER() Over ({0})
  95. As rowNum, * From ({1}) As T ) As N Where rowNum > {2} And rowNum <= {3}
  96. ", orderBy, sql,(pagination.Page-1)*pagination.Rows, pagination.Page * pagination.Rows);
  97. string selectCountSql = "Select Count(*) From (" + sql + ") AS t";
  98. total = (int)db.ExecuteScalar(selectCountSql,para);
  99. return db.Query<T>(sqls, para).ToList();
  100. }
  101. }
  102. /// <summary>
  103. /// 集合查询(Lambda)
  104. /// </summary>
  105. /// <typeparam name="T"></typeparam>
  106. /// <param name="condition"></param>
  107. /// <returns></returns>
  108. public IEnumerable<T> FindList<T>(Expression<Func<T, bool>> condition) where T : class, new()
  109. {
  110. using (var db = Context)
  111. {
  112. return db.Select<List<T>>(condition);
  113. }
  114. }
  115. /// <summary>
  116. /// 分页集合查询(Lambda)
  117. /// </summary>
  118. /// <typeparam name="T"></typeparam>
  119. /// <param name="condition"></param>
  120. /// <returns></returns>
  121. public IEnumerable<T> FindPageList<T>(Expression<Func<T, bool>> condition, PaginationQuery pagination,out int total) where T : class, new()
  122. {
  123. using (var db = Context)
  124. {
  125. return db.Select<T>(condition).QueryPage(pagination.Page,pagination.Rows,out total);
  126. }
  127. }
  128. /// <summary>
  129. /// 查询实体对象
  130. /// </summary>
  131. /// <typeparam name="T"></typeparam>
  132. /// <param name="sql"></param>
  133. /// <param name="para"></param>
  134. /// <returns></returns>
  135. public T FindEntity<T>(string sql, object para = null)
  136. where T : class, new()
  137. {
  138. using (var db = Connection)
  139. {
  140. return db.QueryFirst<T>(sql, para);
  141. }
  142. }
  143. /// <summary>
  144. /// 集合查询(Lambda)
  145. /// </summary>
  146. /// <typeparam name="T"></typeparam>
  147. /// <param name="condition"></param>
  148. /// <returns></returns>
  149. public T FindEntity<T>(Expression<Func<T, bool>> condition) where T : class, new()
  150. {
  151. using (var db = Context)
  152. {
  153. return db.Select<T>(condition).QuerySingle();
  154. }
  155. }
  156. }
  157. }