SqlHelperOA2023.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. using System.Configuration;
  8. namespace DAL
  9. {
  10. public class SqlHelperOA2023
  11. {
  12. //获得数据库连接字符串
  13. public static readonly string conString = ConfigurationManager.ConnectionStrings["sqlConNew"].ConnectionString;
  14. /// <summary>
  15. /// 创建数据库连接对象
  16. /// </summary>
  17. /// <returns>返回数据库连接对象</returns>
  18. public static SqlConnection createCon()
  19. {
  20. return new SqlConnection(conString);
  21. }
  22. public static DataTable QueryAll(string sql)
  23. {
  24. SqlConnection conn = new SqlConnection(conString);
  25. try
  26. {
  27. conn.Open();
  28. SqlCommand com = new SqlCommand(sql, conn);
  29. SqlDataAdapter da = new SqlDataAdapter(com);
  30. DataSet ds = new DataSet();
  31. da.Fill(ds);
  32. return ds.Tables[0];
  33. }
  34. catch (Exception)
  35. {
  36. return null;
  37. }
  38. finally
  39. {
  40. conn.Close();
  41. }
  42. }
  43. /// <summary>
  44. /// 创建执行对象
  45. /// </summary>
  46. /// <param name="cmdText">文本内容</param>
  47. /// <param name="cmdType">文本类型</param>
  48. /// <param name="param">可变参数数组</param>
  49. /// <returns>返回执行对象</returns>
  50. public static SqlCommand createCmd(string cmdText, CommandType cmdType, params SqlParameter[] param)
  51. {
  52. //实例化执行对象
  53. SqlCommand cmd = new SqlCommand();
  54. //执行连接对象
  55. cmd.Connection = createCon();
  56. //执行文本内容
  57. cmd.CommandText = cmdText;
  58. //选择文本类型
  59. cmd.CommandType = cmdType;
  60. //判断可变参数数组里是否为null 如果不为null 则添加进可变参数数组中
  61. if (param != null)
  62. foreach (SqlParameter p in param)
  63. cmd.Parameters.Add(p);
  64. //cmd.Parameters.Clear();//执行完后清理SqlParameter中的值
  65. //返回执行对象
  66. return cmd;
  67. }
  68. /// <summary>
  69. /// 创建ExecuteNonQuery()方法Edit
  70. /// </summary>
  71. /// <param name="cmdText">文本内容</param>
  72. /// <param name="cmdType">文本类型</param>
  73. /// <param name="param">可变参数数组</param>
  74. /// <returns>返回受影响行数</returns>
  75. public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] param)
  76. {
  77. //创建执行对象
  78. SqlCommand cmd = createCmd(cmdText, cmdType, param);
  79. try
  80. {
  81. //打开连接
  82. cmd.Connection.Open();
  83. //执行ExecuteNonQuery()方法
  84. int count = cmd.ExecuteNonQuery();
  85. //关闭连接
  86. cmd.Connection.Close();
  87. //返回受影响行数
  88. return count;
  89. }
  90. catch (Exception e)
  91. {
  92. string error = e.Message.ToString();
  93. //关闭连接
  94. cmd.Connection.Close();
  95. //返回 0
  96. return 0;
  97. }
  98. }
  99. /// <summary>
  100. /// 创建ExecuteScalar()方法Add
  101. /// </summary>
  102. /// <param name="cmdText">文本内容</param>
  103. /// <param name="cmdType">文本类型</param>
  104. /// <param name="param">可变参数数组</param>
  105. /// <returns>返回第1行第1列的值</returns>
  106. public static Object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] param)
  107. {
  108. //创建执行对象
  109. SqlCommand cmd = createCmd(cmdText, cmdType, param);
  110. try
  111. {
  112. //打开连接
  113. cmd.Connection.Open();
  114. //执行查询方法
  115. Object obj = cmd.ExecuteScalar();
  116. //返回第1行第1列的值
  117. return obj;
  118. }
  119. catch (Exception EX)
  120. {
  121. //关闭连接
  122. cmd.Connection.Close();
  123. //返回空
  124. return null;
  125. }
  126. }
  127. }
  128. }