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

296 lines
12 KiB

3 years 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.IO;
  8. using System.Linq;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace NFine.Data.Extensions
  13. {
  14. public class OtherSqlServerHelper
  15. {
  16. #region [ 连接串相关 ]
  17. /// <summary>
  18. /// 数据中心DB的连接字符串
  19. /// </summary>
  20. public static string ConnectionString = SqlHelper.DataCenterConnString;
  21. /// <summary>
  22. /// 获取同步服务器的连接
  23. /// </summary>
  24. /// <returns></returns>
  25. private static SqlConnection GetConnectionString()
  26. {
  27. return new SqlConnection(ConnectionString);
  28. }
  29. /// <summary>
  30. /// 根据连接串获取连接
  31. /// </summary>
  32. /// <returns></returns>
  33. private static void SetConnByString(string conn)
  34. {
  35. ConnectionString = SqlHelper.FromMd5(ConfigurationManager.ConnectionStrings[conn].ConnectionString);
  36. }
  37. #endregion
  38. #region 数据分页
  39. /// <summary>
  40. /// 摘要:
  41. /// 数据分页
  42. /// 参数:
  43. /// sql:传入要执行sql语句
  44. /// param:参数化
  45. /// orderField:排序字段
  46. /// orderType:排序类型
  47. /// pageIndex:当前页
  48. /// pageSize:页大小
  49. /// count:返回查询条数
  50. /// </summary>
  51. public static DataTable GetPageTable(string sql, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  52. {
  53. SetConnByString(connectstr);
  54. StringBuilder strSql = new StringBuilder();
  55. if (pageIndex == 0)
  56. {
  57. pageIndex = 1;
  58. }
  59. int num = (pageIndex - 1) * pageSize;
  60. int num1 = (pageIndex) * pageSize;
  61. string OrderBy = "";
  62. if (!string.IsNullOrEmpty(orderField))
  63. OrderBy = "Order By " + orderField + " " + orderType + "";
  64. else
  65. OrderBy = "order by (select 0)";
  66. strSql.Append("Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  67. strSql.Append(" As rowNum, * From (" + sql + ") As T ) As N Where rowNum > " + num + " And rowNum <= " + num1 + "");
  68. count = Convert.ToInt32(ExecuteScalar(CommandType.Text, "Select Count(1) From (" + sql + ") As t", param));
  69. IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
  70. return DatabaseReader.ReaderToDataTable(dr);
  71. }
  72. /// <summary>
  73. /// 摘要:
  74. /// 数据分页
  75. /// 参数:
  76. /// sql:传入要执行sql语句
  77. /// param:参数化
  78. /// orderField:排序字段
  79. /// orderType:排序类型
  80. /// pageIndex:当前页
  81. /// pageSize:页大小
  82. /// count:返回查询条数
  83. /// </summary>
  84. public static DataTable GetPageTable_EXISTSTemp(string sql, string sqlTempName,string Droptable, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  85. {
  86. SetConnByString(connectstr);
  87. //StringBuilder strSql = new StringBuilder();
  88. if (pageIndex == 0)
  89. {
  90. pageIndex = 1;
  91. }
  92. int num = (pageIndex - 1) * pageSize;
  93. int num1 = (pageIndex) * pageSize;
  94. string OrderBy = "";
  95. if (!string.IsNullOrEmpty(orderField))
  96. OrderBy = "Order By " + orderField + " " + orderType + "";
  97. else
  98. OrderBy = "order by (select 0)";
  99. //strSql.Append(sql+@" Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  100. //strSql.Append(" As rowNum, * From " + sqlTempName + " ) As N Where rowNum > " + num + " And rowNum <= " + num1 + " " + Droptable);
  101. //count = Convert.ToInt32(ExecuteScalar(CommandType.Text, sql + " Select Count(1) From " + sqlTempName +" "+ Droptable, param));
  102. //IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
  103. //return DatabaseReader.ReaderToDataTable(dr);
  104. using (SqlConnection conn = GetConnectionString())
  105. {
  106. conn.Open();
  107. sql += " Select Count(1) From " + sqlTempName;
  108. sql += " Select * From (Select ROW_NUMBER() Over (" + OrderBy + ") ";
  109. sql += " As rowNum, * From " + sqlTempName + " ) As N Where rowNum > " + num + " And rowNum <= " + num1 + " " ;
  110. sql += Droptable;
  111. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  112. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  113. DataSet ds = new DataSet();
  114. adapter.Fill(ds);
  115. count = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
  116. return ds.Tables[1];
  117. }
  118. }
  119. /// <summary>
  120. /// 摘要:
  121. /// 数据分页
  122. /// 参数:
  123. /// sql:传入要执行sql语句
  124. /// param:参数化
  125. /// orderField:排序字段
  126. /// orderType:排序类型
  127. /// pageIndex:当前页
  128. /// pageSize:页大小
  129. /// count:返回查询条数
  130. /// </summary>
  131. public static DataTable GetPageTable_EXISTSTempNew(string sql, string sqlTempName, string Droptable, string connectstr, DbParameter[] param, string orderField, string orderType, int pageIndex, int pageSize, ref int count)
  132. {
  133. SetConnByString(connectstr);
  134. StringBuilder strSql = new StringBuilder();
  135. if (pageIndex == 0)
  136. {
  137. pageIndex = 1;
  138. }
  139. int num = (pageIndex - 1) * pageSize;
  140. int num1 = (pageIndex) * pageSize;
  141. string OrderBy = "";
  142. if (!string.IsNullOrEmpty(orderField))
  143. OrderBy = "Order By " + orderField + " " + orderType + "";
  144. else
  145. OrderBy = "order by (select 0)";
  146. strSql.Append(sql + @" Select * From (Select ROW_NUMBER() Over (" + OrderBy + ")");
  147. strSql.Append(" As rowNum, * From " + sqlTempName + " ) As N Where rowNum > " + num + " And rowNum <= " + num1 + " " + Droptable);
  148. count = Convert.ToInt32(ExecuteScalar(CommandType.Text, sql + " Select Count(1) From " + sqlTempName + " " + Droptable, param));
  149. IDataReader dr = ExecuteReader(CommandType.Text, strSql.ToString(), param);
  150. return DatabaseReader.ReaderToDataTable(dr);
  151. }
  152. /// <summary>
  153. /// 根据sql语句和参数,返回DataTable
  154. /// </summary>
  155. /// <param name="sql">sql语句</param>
  156. /// <param name="spArr">可变参数</param>
  157. /// <returns>DataTable</returns>
  158. public static DataTable GetDataTableBySql(string sql, string connectstr, params SqlParameter[] spArr)
  159. {
  160. SetConnByString(connectstr);
  161. using (SqlConnection conn = GetConnectionString())
  162. {
  163. conn.Open();
  164. SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout();
  165. if (spArr != null && spArr.Length > 0)
  166. cmd.Parameters.AddRange(spArr.SetDBNull());
  167. DataTable dt = cmd.ExecuteDataTable();
  168. return dt;
  169. }
  170. }
  171. /// <summary>
  172. /// 使用提供的参数,执行有结果集返回的数据库操作命令、并返回SqlDataReader对象
  173. /// </summary>
  174. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  175. /// <param name="commandText">存储过程名称或者T-SQL命令行<</param>
  176. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  177. /// <returns>返回SqlDataReader对象</returns>
  178. private static IDataReader ExecuteReader(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  179. {
  180. DbCommand cmd = DbFactory.CreateDbCommand();
  181. cmd.CommandTimeout = 180;
  182. DbConnection conn = DbFactory.CreateDbConnection(ConnectionString);
  183. try
  184. {
  185. PrepareCommand(cmd, conn, null, cmdType, cmdText, parameters);
  186. IDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  187. cmd.Parameters.Clear();
  188. return rdr;
  189. }
  190. catch (Exception ex)
  191. {
  192. conn.Close();
  193. cmd.Dispose();
  194. //log.Error(ex.Message);
  195. throw;
  196. }
  197. }
  198. /// <summary>
  199. /// 依靠数据库连接字符串connectionString,
  200. /// 使用所提供参数,执行返回首行首列命令
  201. /// </summary>
  202. /// <param name="commandType">执行命令的类型(存储过程或T-SQL,等等)</param>
  203. /// <param name="commandText">存储过程名称或者T-SQL命令行</param>
  204. /// <param name="parameters">执行命令所需的sql语句对应参数</param>
  205. /// <returns>返回一个对象,使用Convert.To{Type}将该对象转换成想要的数据类型。</returns>
  206. private static object ExecuteScalar(CommandType cmdType, string cmdText, params DbParameter[] parameters)
  207. {
  208. try
  209. {
  210. DbCommand cmd = DbFactory.CreateDbCommand();
  211. cmd.CommandTimeout = 180;
  212. using (DbConnection connection = DbFactory.CreateDbConnection(ConnectionString))
  213. {
  214. PrepareCommand(cmd, connection, null, cmdType, cmdText, parameters);
  215. object val = cmd.ExecuteScalar();
  216. cmd.Parameters.Clear();
  217. return val;
  218. }
  219. }
  220. catch (Exception ex)
  221. {
  222. //log.Error(ex.Message);
  223. throw;
  224. }
  225. }
  226. /// <summary>
  227. /// 为即将执行准备一个命令
  228. /// </summary>
  229. /// <param name="cmd">SqlCommand对象</param>
  230. /// <param name="conn">SqlConnection对象</param>
  231. /// <param name="isOpenTrans">DbTransaction对象</param>
  232. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  233. /// <param name="cmdText">存储过程名称或者T-SQL命令行, e.g. Select * from Products</param>
  234. /// <param name="cmdParms">SqlParameters to use in the command</param>
  235. private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction isOpenTrans, CommandType cmdType, string cmdText, DbParameter[] cmdParms)
  236. {
  237. if (conn.State != ConnectionState.Open)
  238. conn.Open();
  239. cmd.Connection = conn;
  240. cmd.CommandText = cmdText;
  241. if (isOpenTrans != null)
  242. cmd.Transaction = isOpenTrans;
  243. cmd.CommandType = cmdType;
  244. if (cmdParms != null)
  245. {
  246. cmd.Parameters.AddRange(cmdParms);
  247. }
  248. }
  249. #endregion
  250. /// <summary>
  251. /// 执行 SQL 语句,并返回受影响的行数。
  252. /// </summary>
  253. /// <param name="cmdType">执行命令的类型(存储过程或T-SQL,等等)</param>
  254. /// <param name="cmdText">存储过程名称或者T-SQL命令行</param>
  255. /// <returns></returns>
  256. public static int ExecuteNonQuery_Other(CommandType cmdType, string cmdText, string Connect)
  257. {
  258. int num = 0;
  259. SetConnByString(Connect);
  260. try
  261. {
  262. DbCommand cmd = DbFactory.CreateDbCommand();
  263. using (DbConnection conn = DbFactory.CreateDbConnection(ConnectionString))
  264. {
  265. PrepareCommand(cmd, conn, null, cmdType, cmdText, null);
  266. num = cmd.ExecuteNonQuery();
  267. cmd.Parameters.Clear();
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. num = -1;
  273. //log.Error(ex.Message);
  274. }
  275. return num;
  276. }
  277. }
  278. }