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.

295 lines
12 KiB

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