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.

119 lines
3.7 KiB

2 weeks ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace NFine.Application
  9. {
  10. public class SqlCommandHelper
  11. {
  12. public static void CmdExecuteNonQuery(string sql, SqlCommand cmd)
  13. {
  14. try
  15. {
  16. cmd.CommandText = sql;
  17. int count = cmd.ExecuteNonQuery();
  18. if (count <= 0)
  19. {
  20. throw new Exception("受影响行数小于0");
  21. }
  22. cmd.Parameters.Clear();
  23. sql = string.Empty;
  24. }
  25. catch (Exception ex)
  26. {
  27. throw new Exception(ex.Message);
  28. }
  29. }
  30. public static void CmdExecuteNonQueryByUpdate(string sql, SqlCommand cmd)
  31. {
  32. try
  33. {
  34. cmd.CommandText = sql;
  35. int count = cmd.ExecuteNonQuery();
  36. //if (count <= 0)
  37. //{
  38. // throw new Exception("受影响行数小于0");
  39. //}
  40. cmd.Parameters.Clear();
  41. sql = string.Empty;
  42. }
  43. catch (Exception ex)
  44. {
  45. throw new Exception(ex.Message);
  46. }
  47. }
  48. public static DataSet SQlReturnDataSet(string SQl, SqlCommand cmd)
  49. {
  50. DataSet dt = new DataSet();
  51. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  52. cmd.CommandText = SQl;
  53. dr.SelectCommand = cmd;
  54. dr.Fill(dt);
  55. return dt;
  56. }
  57. public static void CmdExecuteNonQueryBYvendor(string sql, SqlCommand cmd)
  58. {
  59. try
  60. {
  61. cmd.CommandText = sql;
  62. int count = cmd.ExecuteNonQuery();
  63. if (count <= 0)
  64. {
  65. throw new Exception("受影响行数小于0");
  66. }
  67. cmd.Parameters.Clear();
  68. sql = string.Empty;
  69. }
  70. catch (Exception ex)
  71. {
  72. throw new Exception(ex.Message);
  73. }
  74. }
  75. public static void CmdExecuteNonQuery(string sql, SqlParameter[] sp, SqlCommand cmd)
  76. {
  77. try
  78. {
  79. foreach (SqlParameter parameter in sp)
  80. {
  81. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  82. (parameter.Value == null))
  83. {
  84. parameter.Value = DBNull.Value;
  85. }
  86. cmd.Parameters.Add(parameter);
  87. }
  88. cmd.CommandText = sql;
  89. int count = cmd.ExecuteNonQuery();
  90. if (count <= 0)
  91. {
  92. throw new Exception("受影响行数小于0");
  93. }
  94. cmd.Parameters.Clear();
  95. sql = string.Empty;
  96. }
  97. catch (Exception ex)
  98. {
  99. throw new Exception(ex.Message);
  100. }
  101. }
  102. /// <summary>
  103. /// 事物取DataTable
  104. /// </summary>
  105. /// <param name="SQl"></param>
  106. /// <param name="cmd"></param>
  107. /// <returns></returns>
  108. public static DataTable SQlReturnData(string SQl, SqlCommand cmd)
  109. {
  110. DataTable dt = new DataTable();
  111. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  112. cmd.CommandText = SQl;
  113. dr.SelectCommand = cmd;
  114. dr.Fill(dt);
  115. return dt;
  116. }
  117. }
  118. }