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

99 lines
3.0 KiB

3 years 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 ICS.Data
  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 DeleteCmdExecuteNonQuery(string sql, SqlCommand cmd)
  31. {
  32. try
  33. {
  34. cmd.CommandText = sql;
  35. int count = cmd.ExecuteNonQuery();
  36. cmd.Parameters.Clear();
  37. sql = string.Empty;
  38. }
  39. catch (Exception ex)
  40. {
  41. throw new Exception(ex.Message);
  42. }
  43. }
  44. public static void CmdExecuteNonQuery(string sql, SqlParameter[] sp, SqlCommand cmd)
  45. {
  46. try
  47. {
  48. foreach (SqlParameter parameter in sp)
  49. {
  50. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
  51. (parameter.Value == null))
  52. {
  53. parameter.Value = DBNull.Value;
  54. }
  55. cmd.Parameters.Add(parameter);
  56. }
  57. cmd.CommandText = sql;
  58. int count = cmd.ExecuteNonQuery();
  59. if (count <= 0)
  60. {
  61. throw new Exception("受影响行数小于0");
  62. }
  63. cmd.Parameters.Clear();
  64. sql = string.Empty;
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new Exception(ex.Message);
  69. }
  70. }
  71. /// <summary>
  72. /// 事物取DataTable
  73. /// </summary>
  74. /// <param name="SQl"></param>
  75. /// <param name="cmd"></param>
  76. /// <returns></returns>
  77. public static DataTable SQlReturnData(string SQl, SqlCommand cmd)
  78. {
  79. DataTable dt = new DataTable();
  80. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  81. cmd.CommandText = SQl;
  82. dr.SelectCommand = cmd;
  83. dr.Fill(dt);
  84. return dt;
  85. }
  86. public static DataSet SQlReturnDataSet(string SQl, SqlCommand cmd)
  87. {
  88. DataSet dt = new DataSet();
  89. SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter();
  90. cmd.CommandText = SQl;
  91. dr.SelectCommand = cmd;
  92. dr.Fill(dt);
  93. return dt;
  94. }
  95. }
  96. }