圣珀
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.

141 lines
4.1 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace ICS.WCF.Base
  9. {
  10. public class DBhlper
  11. {
  12. public static DataTable Query(string sql,string sqlconn)
  13. {
  14. try
  15. {
  16. if (sqlconn == "")
  17. {
  18. throw new Exception("连接U9字符串取得失败");
  19. }
  20. SqlConnection conn = new SqlConnection(sqlconn);
  21. SqlDataAdapter da = new SqlDataAdapter(sql, conn);
  22. DataTable dt = new DataTable();
  23. da.Fill(dt);
  24. return dt;
  25. }
  26. catch (Exception ex)
  27. {
  28. throw ex;
  29. }
  30. }
  31. public static string getDataSource( string sqlconn)
  32. {
  33. try
  34. {
  35. if (sqlconn == "")
  36. {
  37. throw new Exception("连接U9字符串取得失败");
  38. }
  39. SqlConnection conn = new SqlConnection(sqlconn);
  40. return "["+conn.DataSource+"].["+conn.Database+"]";
  41. }
  42. catch (Exception ex)
  43. {
  44. throw ex;
  45. }
  46. }
  47. public static bool ReaerEXecute(string sql, string sqlconn)
  48. {
  49. try
  50. {
  51. SqlConnection conn = new SqlConnection(sqlconn);
  52. SqlCommand cmd = new SqlCommand(sql, conn);
  53. conn.Open();
  54. SqlDataReader a = cmd.ExecuteReader();
  55. //conn.Close();
  56. return a.HasRows;
  57. }
  58. catch (Exception ex)
  59. {
  60. throw ex;
  61. }
  62. }
  63. public static int EXecute(string sql,string sqlconn)
  64. {
  65. try
  66. {
  67. SqlConnection conn = new SqlConnection(sqlconn);
  68. SqlCommand cmd = new SqlCommand(sql, conn);
  69. conn.Open();
  70. int a = cmd.ExecuteNonQuery();
  71. conn.Close();
  72. return a;
  73. }
  74. catch (Exception ex)
  75. {
  76. throw ex;
  77. }
  78. }
  79. public static void SQLRunAll(List<string> SqlAll, string sqlconn)
  80. {
  81. try
  82. {
  83. if (sqlconn == "")
  84. {
  85. throw new Exception("连接U9字符串取得失败");
  86. }
  87. SqlConnection connetion = new SqlConnection(sqlconn); //创建数据库连接
  88. SqlTransaction transaction = null; //声明事务
  89. SqlCommand command = null;
  90. connetion.Open(); //打开数据库连接
  91. transaction = connetion.BeginTransaction(); //使用数据库连接创建事务
  92. command = connetion.CreateCommand();
  93. command.Transaction = transaction;
  94. try
  95. {
  96. for (int i = 0; i < SqlAll.Count; i++)
  97. {
  98. if (!string.IsNullOrEmpty(SqlAll[i])) //判断当前SQL语句是否为空
  99. {
  100. command.CommandText = SqlAll[i];
  101. command.ExecuteNonQuery();
  102. }
  103. }
  104. transaction.Commit(); //事务提交
  105. }
  106. catch (Exception ex)
  107. {
  108. transaction.Rollback(); //事务撤销
  109. throw new Exception();
  110. }
  111. finally
  112. {
  113. connetion.Close();
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. throw new Exception();
  119. }
  120. }
  121. }
  122. }