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.

171 lines
5.3 KiB

преди 3 години
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. using System.Text;
  8. /// <summary>
  9. ///DBacess 的摘要说明
  10. /// </summary>
  11. namespace basic
  12. {
  13. public class DBacess
  14. {
  15. /// <summary>
  16. /// 创建活动SqlConnection
  17. /// </summary>
  18. /// <returns>SqlConnection</returns>
  19. ///
  20. public static SqlConnection con;
  21. private static SqlConnection connection()
  22. {
  23. SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString());
  24. con = conn;
  25. return conn;
  26. }
  27. /// <summary>
  28. /// 打开连接数据库
  29. /// </summary>
  30. /// <param name="conn">SqlConnection对象</param>
  31. private void OpenConnection(SqlConnection conn)
  32. {
  33. if (conn != null && conn.State == ConnectionState.Closed)
  34. conn.Open();
  35. }
  36. /// <summary>
  37. /// 关闭数据库
  38. /// </summary>
  39. /// <param name="conn">SqlConnection对象</param>
  40. private void CloseConnection(SqlConnection conn)
  41. {
  42. if (conn != null && conn.State == ConnectionState.Open)
  43. conn.Close();
  44. }
  45. /// <summary>
  46. /// 更新数据库
  47. /// </summary>
  48. /// <param name="sql"></param>
  49. /// <param name="parameters"></param>
  50. /// <param name="type"></param>
  51. /// <returns></returns>
  52. public bool Update(string sql, SqlParameter[] parameters, CommandType type)
  53. {
  54. SqlConnection conn = connection();
  55. bool result = false;
  56. try
  57. {
  58. using (conn)
  59. {
  60. SqlCommand cmd = new SqlCommand(sql, conn);
  61. if (parameters != null)
  62. {
  63. foreach (SqlParameter parameter in parameters)
  64. {
  65. if (parameters != null && parameter.ParameterName != "")
  66. {
  67. cmd.Parameters.Add(parameter);
  68. }
  69. }
  70. }
  71. cmd.CommandType = type;
  72. cmd.Connection.Open();
  73. int flag = cmd.ExecuteNonQuery();
  74. if (flag > 0)
  75. result = true;
  76. }
  77. }
  78. finally
  79. {
  80. CloseConnection(conn);
  81. }
  82. return result;
  83. }
  84. /// <summary>
  85. /// 根据条件查询数据
  86. /// </summary>
  87. /// <param name="sql"></param>
  88. /// <param name="parameters"></param>
  89. /// <returns>DataTable</returns>
  90. public DataTable SelectReturnDataTable(string sql, SqlParameter[] parameters)
  91. {
  92. SqlConnection conn = connection();
  93. DataTable dt = new DataTable();
  94. try
  95. {
  96. using (conn)
  97. {
  98. SqlCommand cmd = new SqlCommand(sql, conn);
  99. if (parameters != null)
  100. {
  101. foreach (SqlParameter parameter in parameters)
  102. {
  103. if (parameter != null && parameter.ParameterName != "")
  104. {
  105. cmd.Parameters.Add(parameter);
  106. }
  107. }
  108. }
  109. conn.Open();
  110. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  111. sda.Fill(dt);
  112. }
  113. }
  114. //catch (Exception e)
  115. //{
  116. //}
  117. finally
  118. {
  119. CloseConnection(conn);
  120. }
  121. return dt;
  122. }
  123. /// <summary>
  124. /// 根据条件查询数据
  125. /// </summary>
  126. /// <param name="sql"></param>
  127. /// <param name="parameters"></param>
  128. /// <returns>DataSet</returns>
  129. public DataSet SelectReturnDataSet(string sql, SqlParameter[] parameters)
  130. {
  131. SqlConnection conn = connection();
  132. DataSet ds = new DataSet();
  133. try
  134. {
  135. using (conn)
  136. {
  137. SqlCommand cmd = new SqlCommand(sql, conn);
  138. if (parameters != null)
  139. {
  140. foreach (SqlParameter parameter in parameters)
  141. {
  142. if (parameter != null && parameter.ParameterName != "")
  143. {
  144. cmd.Parameters.Add(parameter);
  145. }
  146. }
  147. }
  148. conn.Open();
  149. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  150. sda.Fill(ds);
  151. }
  152. }
  153. //catch (Exception e)
  154. //{
  155. //}
  156. finally
  157. {
  158. CloseConnection(conn);
  159. }
  160. return ds;
  161. }
  162. }
  163. }