SQL Server安装包

image.png

MySQL安装包

image.png

全部代码

mysql和sql server的操作基本一致,唯一不一样的就是各种名称空间,类名不太一样,如:SqlCommand和MySqlCommand,Sql开头的变成MySql开头 这里连接的是sql server数据库

数据库链接字符串

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Text;
  7. /// <summary>
  8. /// MSSQL数据库访问
  9. ///
  10. /// </summary>
  11. public abstract class SqlHelper
  12. {
  13. #region 连接字符串
  14. private static string connectionString = "Data Source=.;Initial Catalog=S;Integrated Security=True";//连接字符串
  15. #endregion
  16. #region 初始化连接数据库
  17. /// <summary>
  18. /// 设置SqlCommand命令
  19. /// </summary>
  20. /// <param name="cmd">sql命令对象</param>
  21. /// <param name="conn">sql连接对象</param>
  22. /// <param name="trans">sql事务对象</param>
  23. /// <param name="cmdText">sql语句</param>
  24. /// <param name="cmdParms">sql参数</param>
  25. private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
  26. {
  27. if (conn.State != ConnectionState.Open)
  28. conn.Open();
  29. cmd.Connection = conn;
  30. cmd.CommandText = cmdText;
  31. if (trans != null)
  32. cmd.Transaction = trans;
  33. cmd.CommandType = CommandType.Text;
  34. if (cmdParms != null)
  35. {
  36. foreach (SqlParameter parm in cmdParms)
  37. cmd.Parameters.Add(parm);
  38. }
  39. }
  40. #endregion
  41. #region 执行操作
  42. /*
  43. * SqlParameter用法:
  44. * 例如:string sql = "insert into T values(@name)";
  45. * SqlParameter name = new SqlParameter("name","取一个名字");
  46. * ExecuteSql(sql,name)
  47. * ExecuteSql(sql,new SqlParameter("name","取一个名字"))
  48. * 没有参数可以不写cmdParms
  49. */
  50. /// <summary>
  51. /// 执行sql操作,一般insert,update,delete
  52. /// </summary>
  53. /// <param name="sql">sql字符串</param>
  54. /// <param name="cmdParms">sql参数,可以为空</param>
  55. /// <returns>返回受影响的行数</returns>
  56. public static int ExecuteSql(string sql, params SqlParameter[] cmdParms)
  57. {
  58. using(SqlConnection connection=new SqlConnection(connectionString))
  59. {
  60. using(SqlCommand cmd=new SqlCommand())
  61. {
  62. try
  63. {
  64. PrepareCommand(cmd, connection, null, sql, cmdParms);
  65. int rows = cmd.ExecuteNonQuery();
  66. cmd.Parameters.Clear();
  67. return rows;
  68. }
  69. catch (System.Data.SqlClient.SqlException E)
  70. {
  71. throw new Exception(E.Message);
  72. }
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 得到第一行第一列的值,可用于计算count,max,min,avg,sum等
  78. /// </summary>
  79. /// <param name="sql">sql字符串</param>
  80. /// <param name="cmdParms">sql参数,可以为空</param>
  81. /// <returns>返回查询到的结果object类型</returns>
  82. public static object GetFrist(string sql, params SqlParameter[] cmdParms)
  83. {
  84. using (SqlConnection connection = new SqlConnection(connectionString))
  85. {
  86. using (SqlCommand cmd = new SqlCommand())
  87. {
  88. try
  89. {
  90. PrepareCommand(cmd, connection, null, sql, cmdParms);
  91. object obj = cmd.ExecuteScalar();
  92. cmd.Parameters.Clear();
  93. if (Equals(obj, null) || Equals(obj, System.DBNull.Value))//判断是否返回的值为空
  94. {
  95. return null;
  96. }
  97. else
  98. {
  99. return obj;
  100. }
  101. }
  102. catch (System.Data.SqlClient.SqlException e)
  103. {
  104. throw new Exception(e.Message);
  105. }
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// 得到一个reader对象,遍历这个对象获得数据
  111. /// 用法:while(reader.Read())
  112. /// {
  113. /// int id=reader.GetInt32(0);//列的索引
  114. /// int id=(int)reader[0]
  115. /// int id=(int)reader["id"]
  116. /// }
  117. /// </summary>
  118. /// <param name="sql">sql字符串</param>
  119. /// <param name="cmdParms">sql参数,可以为空</param>
  120. /// <returns>reader对象</returns>
  121. public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] cmdParms)
  122. {
  123. using (SqlConnection connection = new SqlConnection(connectionString))
  124. {
  125. using (SqlCommand cmd = new SqlCommand())
  126. {
  127. try
  128. {
  129. PrepareCommand(cmd, connection, null, sql, cmdParms);
  130. SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  131. cmd.Parameters.Clear();
  132. return reader;
  133. }
  134. catch (System.Data.SqlClient.SqlException e)
  135. {
  136. throw new Exception(e.Message);
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 得到一个DataTable对象,实现查询数据
  143. /// 用法:dt.Rows[5][0]或者dt.Rows[5]["id"]等
  144. /// </summary>
  145. /// <param name="sql">sql字符串</param>
  146. /// <param name="cmdParms">sql参数,可以为空</param>
  147. /// <returns>返回一个datatable对象</returns>
  148. public static DataTable GetDataTable(string sql, params SqlParameter[] cmdParms)
  149. {
  150. using (SqlConnection connection = new SqlConnection(connectionString))
  151. {
  152. using(SqlCommand cmd = new SqlCommand())
  153. {
  154. PrepareCommand(cmd, connection, null, sql, cmdParms);
  155. using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  156. {
  157. DataTable dt = new DataTable();
  158. try
  159. {
  160. da.Fill(dt);
  161. cmd.Parameters.Clear();
  162. return dt;
  163. }
  164. catch (System.Data.SqlClient.SqlException ex)
  165. {
  166. throw new Exception(ex.Message);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// 执行sql事务
  174. /// 确保事务发生的一致性,都成功了就提交,失败异常就回滚
  175. /// 如果没有参数,哈希表的value写null
  176. /// 执行sql如果返回0行也叫执行成功了
  177. /// </summary>
  178. /// <param name="SQLStringList">哈希表,key为sql语句,value为SqlParameter[]</param>
  179. public static void ExecuteSqlTran(Hashtable SQLStringList)
  180. {
  181. using (SqlConnection conn = new SqlConnection(connectionString))
  182. {
  183. conn.Open();
  184. using (SqlTransaction trans = conn.BeginTransaction())
  185. {
  186. using(SqlCommand cmd = new SqlCommand())
  187. {
  188. try
  189. {
  190. //循环
  191. foreach (DictionaryEntry myDE in SQLStringList)
  192. {
  193. string sql = myDE.Key.ToString();
  194. SqlParameter[] cmdParms = (SqlParameter[])myDE.Value;
  195. PrepareCommand(cmd, conn, trans, sql, cmdParms);
  196. int val = cmd.ExecuteNonQuery();
  197. cmd.Parameters.Clear();
  198. }
  199. trans.Commit();
  200. }
  201. catch
  202. {
  203. trans.Rollback();
  204. throw;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /// <summary>
  211. /// 判断是否存在记录
  212. /// </summary>
  213. /// <param name="sql">sql语句</param>
  214. /// <param name="cmdParms">sql参数,可以为空</param>
  215. /// <returns></returns>
  216. public static bool Exists(string sql, params SqlParameter[] cmdParms)
  217. {
  218. try
  219. {
  220. if (GetDataTable(sql, cmdParms).Rows.Count > 0)
  221. {
  222. return true;
  223. }
  224. else
  225. {
  226. return false;
  227. }
  228. }
  229. catch (System.Data.SqlClient.SqlException E)
  230. {
  231. throw new Exception(E.Message);
  232. }
  233. }
  234. #endregion
  235. }