• 数据库访问工具类


    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Configuration;
    7. using System.Data;
    8. using System.Data.SqlClient;
    9. using System.Reflection;
    10. namespace StudentDLL
    11. {
    12. public class Datebase
    13. {
    14. private static string _sqlserver = ConfigurationManager.ConnectionStrings["sqlserver"].ToString();
    15. private SqlConnection conn;
    16. /// <summary>
    17. /// 初始化conn并且打开sqlconnection
    18. /// </summary>
    19. public SqlConnection Conn
    20. {
    21. get
    22. {
    23. if (conn == null)
    24. {
    25. conn = new SqlConnection(_sqlserver);
    26. }
    27. if (conn.State == ConnectionState.Closed)
    28. {
    29. conn.Open();
    30. }
    31. else if (conn.State == ConnectionState.Broken)
    32. {
    33. conn.Close();
    34. conn.Open();
    35. }
    36. return conn;
    37. }
    38. }
    39. #region 关闭数据库连接
    40. public void CloseSqlconnection()
    41. {
    42. if (conn.State == ConnectionState.Open || conn.State == ConnectionState.Broken)
    43. {
    44. conn.Close();
    45. }
    46. }
    47. #endregion
    48. #region 查询
    49. /// <summary>
    50. /// 使用T-SQL语句查询
    51. /// </summary>
    52. /// <param name="sql"></param>
    53. /// <returns></returns>
    54. public DataSet executeReader(string sql)
    55. {
    56. DataSet ds = new DataSet();
    57. using(SqlDataAdapter da=new SqlDataAdapter(sql,conn)){da.Fill(ds);}
    58. CloseSqlconnection();
    59. return ds;
    60. }
    61. /// <summary>
    62. /// T-Sql参数化查询
    63. /// </summary>
    64. /// <param name="sql"></param>
    65. /// <param name="par"></param>
    66. /// <returns></returns>
    67. public DataSet executeReader(string sql, params SqlParameter[] par)
    68. {
    69. DataSet ds = new DataSet();
    70. using (SqlDataAdapter da = new SqlDataAdapter(sql, Conn))
    71. {
    72. da.SelectCommand.Parameters.AddRange(par);
    73. da.Fill(ds);
    74. }
    75. CloseSqlconnection();
    76. return ds;
    77. }
    78. /// <summary>
    79. /// 不带参数存储过程查询
    80. /// </summary>
    81. /// <param name="procName">存储过程名称</param>
    82. /// <returns>DataSet表</returns>
    83. public DataSet procExecuteReader(string procName)
    84. {
    85. DataSet ds = new DataSet();
    86. using (SqlDataAdapter dar = new SqlDataAdapter(procName, Conn))
    87. {
    88. dar.SelectCommand.CommandType = CommandType.StoredProcedure;
    89. dar.Fill(ds);
    90. }
    91. CloseSqlconnection();
    92. return ds;
    93. }
    94. /// <summary>
    95. /// 参数化存储过程查询
    96. /// </summary>
    97. /// <param name="procName">存储过程名称</param>
    98. /// <param name="par"></param>
    99. /// <returns>DataSet</returns>
    100. public DataSet procExecuteReader(string procName,params SqlParameter [] par)
    101. {
    102. DataSet ds = new DataSet();
    103. using (SqlDataAdapter dar = new SqlDataAdapter(procName,Conn))
    104. {
    105. dar.SelectCommand.CommandType = CommandType.StoredProcedure;
    106. dar.SelectCommand.Parameters.AddRange(par);
    107. dar.Fill(ds);
    108. }
    109. CloseSqlconnection();
    110. return ds;
    111. }
    112. #endregion
    113. #region 增删改
    114. /// <summary>
    115. /// 使用sql语句增删改
    116. /// </summary>
    117. /// <param name="sql"></param>
    118. /// <returns></returns>
    119. public int executeNoQuery(string sql)
    120. {
    121. int dr = 0;
    122. using (SqlCommand comm = new SqlCommand(sql, Conn))
    123. {
    124. dr = comm.ExecuteNonQuery();
    125. }
    126. CloseSqlconnection();
    127. return dr;
    128. }
    129. /// <summary>
    130. /// 使用参数化增删改
    131. /// </summary>
    132. /// <param name="sql"></param>
    133. /// <param name="obj"></param>
    134. /// <returns></returns>
    135. public int executeNoQuery(string sql,params SqlParameter [] par)
    136. {
    137. int dr = 0;
    138. using (SqlCommand comm = new SqlCommand(sql, Conn))
    139. {
    140. comm.Parameters.AddRange(par);
    141. dr = comm.ExecuteNonQuery();
    142. }
    143. CloseSqlconnection();
    144. return dr;
    145. }
    146. /// <summary>
    147. /// 使用存储过程增删改
    148. /// </summary>
    149. /// <returns></returns>
    150. public int procExecuteNoQuery(string procName,params SqlParameter[] par)
    151. {
    152. int result = 0;
    153. using (SqlCommand comm = new SqlCommand(procName,Conn))
    154. {
    155. comm.CommandType = CommandType.StoredProcedure;
    156. comm.Parameters.AddRange(par);
    157. result = comm.ExecuteNonQuery();
    158. }
    159. CloseSqlconnection();
    160. return result;
    161. }
    162. #endregion
    163. }
    164. }



  • 相关阅读:
    linux部署docker镜像
    SpringBoot 定时任务篇
    POST形式 soapUI调用WebService的restful接口,传入json参数,并且返回json
    Java操作FTP工具类(实例详解)
    MyBatis逆向工程:根据table生成Model、Mapper、Mapper.xml
    Netty完成网络通信(二)
    NIO完成网络通信(一)
    MySQL5.6数据库8小时内无请求自动断开连接
    Eclipse集成Tomcat插件(特别简单)
    程序从sqlserver2008搬家到MySQL5.6
  • 原文地址:https://www.cnblogs.com/BookCode/p/ec47195a9d16abd4798d7677ea3d72ea.html
Copyright © 2020-2023  润新知