纽威
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.

178 lines
6.9 KiB

1 year ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Data.SqlClient;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace NFine.Data.Extensions
  11. {
  12. public class ERPSqlServerHelper
  13. {
  14. #region [ 连接串相关 ]
  15. /// <summary>
  16. /// 数据中心DB的连接字符串
  17. /// </summary>
  18. public static string ConnectionString = ConfigurationManager.ConnectionStrings["ERPconnstr"].ConnectionString;
  19. /// <summary>
  20. /// 获取同步服务器的连接
  21. /// </summary>
  22. /// <returns></returns>
  23. private static SqlConnection GetConnectionString()
  24. {
  25. return new SqlConnection(ConnectionString);
  26. }
  27. /// <summary>
  28. /// 根据连接串获取连接
  29. /// </summary>
  30. /// <returns></returns>
  31. private static SqlConnection GetConnByString(string conn)
  32. {
  33. return new SqlConnection(conn);
  34. }
  35. #endregion
  36. #region 数据分页
  37. /// <summary>
  38. /// 摘要:
  39. /// 数据分页
  40. /// 参数:
  41. /// sql:传入要执行sql语句
  42. /// param:参数化
  43. /// orderField:排序字段
  44. /// orderType:排序类型
  45. /// pageIndex:当前页
  46. /// pageSize:页大小
  47. /// count:返回查询条数
  48. /// </summary>
  49. public static DataTable GetPageTable(string sql, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  50. {
  51. StringBuilder strSql = new StringBuilder();
  52. if (pageIndex == 0)
  53. {
  54. pageIndex = 1;
  55. }
  56. int num = (pageIndex - 1) * pageSize;
  57. int num1 = (pageIndex) * pageSize;
  58. string OrderBy = "";
  59. if (!string.IsNullOrEmpty(orderField))
  60. OrderBy = "Order By " + orderField + " " + orderType + "";
  61. else
  62. OrderBy = "order by (select 0)";
  63. strSql.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  64. strSql.Append(" As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
  65. count = Convert.ToInt32(ExecuteScalar(CommandType.Text, "Select Count(1) From (" + sql + ") As t", param));
  66. IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
  67. return DatabaseReader.ReaderToDataTable(dr);
  68. }
  69. /// <summary>
  70. /// 根据sql语句和参数,返回DataTable
  71. /// </summary>
  72. /// <param name="sql">sql语句</param>
  73. /// <param name="spArr">可变参数</param>
  74. /// <returns>DataTable</returns>
  75. public static DataTable GetDataTableBySql(string sql, params SqlParameter[] spArr)
  76. {
  77. using (SqlConnection conn = GetConnectionString())
  78. {
  79. conn.Open();
  80. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  81. if (spArr != null && spArr.Length > 0)
  82. cmd.Parameters.AddRange(spArr.SetDBNull());
  83. DataTable dt = cmd.ExecuteDataTable();
  84. return dt;
  85. }
  86. }
  87. /// <summary>
  88. /// 使用提供的参数,执行有结果集返回的数据库操作命令、并返回SqlDataReader对象
  89. /// </summary>
  90. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  91. /// <param name="commandText">存储过程名称或者T-SQL命令行<</param>
  92. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  93. /// <returns>返回SqlDataReader对象</returns>
  94. private static IDataReader ExecuteReader(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  95. {
  96. DbCommand cmd = DbFactory.CreateDbCommand();
  97. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  98. try
  99. {
  100. PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
  101. IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  102. cmd.Parameters.Clear();
  103. return rdr;
  104. }
  105. catch (Exception ex)
  106. {
  107. conn.Close();
  108. cmd.Dispose();
  109. //log.Error(ex.Message);
  110. throw;
  111. }
  112. }
  113. /// <summary>
  114. /// 依靠数据库连接字符串connectionString,
  115. /// 使用所提供参数,执行返回首行首列命令
  116. /// </summary>
  117. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  118. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  119. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  120. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  121. private static object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  122. {
  123. try
  124. {
  125. DbCommand cmd = DbFactory.CreateDbCommand();
  126. using (DbConnection connection = DbFactory.CreateDbConnection(ConnectionString))
  127. {
  128. PrepareCommand(cmd, connection, null, cmdType, cmdText, parameters);
  129. object val = cmd.ExecuteScalar();
  130. cmd.Parameters.Clear();
  131. return val;
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. //log.Error(ex.Message);
  137. throw;
  138. }
  139. }
  140. /// <summary>
  141. /// 为即将执行准备一个命令
  142. /// </summary>
  143. /// <param name="cmd">SqlCommand对象</param>
  144. /// <param name="conn">SqlConnection对象</param>
  145. /// <param name="isOpenTrans">DbTransaction对象</param>
  146. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  147. /// <param name="cmdText">存储过程名称或者T-SQL命令行, e.g. Select * from Products</param>
  148. /// <param name="cmdParms">SqlParameters to use in the command</param>
  149. private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
  150. {
  151. if (conn.State != ConnectionState.Open)
  152. conn.Open();
  153. cmd.Connection = conn;
  154. cmd.CommandText = cmdText;
  155. if (isOpenTrans != null)
  156. cmd.Transaction = isOpenTrans;
  157. cmd.CommandType = cmdType;
  158. if (cmdParms != null)
  159. {
  160. cmd.Parameters.AddRange(cmdParms);
  161. }
  162. }
  163. #endregion
  164. }
  165. }