C# mysql 数据库操作模板
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using MySql.Data; 6 using MySql.Data.MySqlClient; 7 using System.Data; 8 using System.Collections; 9 /* 10 * <summary> 11 * title:mysql 数据库连接类 12 * date:2015-1-5 13 * version:MySql.Data5.1.5.0 14 * author: 15 * </summary> 16 */ 17 namespace readMysql 18 { 19 public abstract class DbManager 20 { 21 //数据库连接字符串 22 public static string dbUrl = "DataBase='test';Data Source='localhost';User Id='root';Password='root';charset='utf8';pooling=true"; 23 // 用于缓存参数的HASH表 24 private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); 25 26 /// <summary> 27 /// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集) 28 /// </summary> 29 /// <param name="connectionString">一个有效的连接字符串</param> 30 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 31 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 32 /// <param name="commandParameters">执行命令所用参数的集合</param> 33 /// <returns>执行命令所影响的行数</returns> 34 public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 35 { 36 37 MySqlCommand cmd = new MySqlCommand(); 38 39 using (MySqlConnection conn = new MySqlConnection(connectionString)) 40 { 41 PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); 42 int val = cmd.ExecuteNonQuery(); 43 cmd.Parameters.Clear(); 44 return val; 45 } 46 } 47 48 /// <summary> 49 /// 用现有的数据库连接执行一个sql命令(不返回数据集) 50 /// </summary> 51 /// <param name="connection">一个现有的数据库连接</param> 52 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 53 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 54 /// <param name="commandParameters">执行命令所用参数的集合</param> 55 /// <returns>执行命令所影响的行数</returns> 56 public static int ExecuteNonQuery(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 57 { 58 59 MySqlCommand cmd = new MySqlCommand(); 60 61 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 62 int val = cmd.ExecuteNonQuery(); 63 cmd.Parameters.Clear(); 64 return val; 65 } 66 67 /// <summary> 68 ///使用现有的SQL事务执行一个sql命令(不返回数据集) 69 /// </summary> 70 /// <remarks> 71 ///举例: 72 /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); 73 /// </remarks> 74 /// <param name="trans">一个现有的事务</param> 75 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 76 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 77 /// <param name="commandParameters">执行命令所用参数的集合</param> 78 /// <returns>执行命令所影响的行数</returns> 79 public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 80 { 81 MySqlCommand cmd = new MySqlCommand(); 82 PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); 83 int val = cmd.ExecuteNonQuery(); 84 cmd.Parameters.Clear(); 85 return val; 86 } 87 88 /// <summary> 89 /// 用执行的数据库连接执行一个返回数据集的sql命令 90 /// </summary> 91 /// <remarks> 92 /// 举例: 93 /// MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); 94 /// </remarks> 95 /// <param name="connectionString">一个有效的连接字符串</param> 96 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 97 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 98 /// <param name="commandParameters">执行命令所用参数的集合</param> 99 /// <returns>包含结果的读取器</returns> 100 public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 101 { 102 //创建一个MySqlCommand对象 103 MySqlCommand cmd = new MySqlCommand(); 104 //创建一个MySqlConnection对象 105 MySqlConnection conn = new MySqlConnection(connectionString); 106 107 //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, 108 //因此commandBehaviour.CloseConnection 就不会执行 109 try 110 { 111 //调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数 112 PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); 113 //调用 MySqlCommand 的 ExecuteReader 方法 114 MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); 115 //清除参数 116 cmd.Parameters.Clear(); 117 return reader; 118 } 119 catch 120 { 121 //关闭连接,抛出异常 122 conn.Close(); 123 throw; 124 } 125 } 126 127 /// <summary> 128 /// 返回DataSet 129 /// </summary> 130 /// <param name="connectionString">一个有效的连接字符串</param> 131 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 132 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 133 /// <param name="commandParameters">执行命令所用参数的集合</param> 134 /// <returns></returns> 135 public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 136 { 137 //创建一个MySqlCommand对象 138 MySqlCommand cmd = new MySqlCommand(); 139 //创建一个MySqlConnection对象 140 MySqlConnection conn = new MySqlConnection(connectionString); 141 142 //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, 143 144 try 145 { 146 //调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数 147 PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); 148 //调用 MySqlCommand 的 ExecuteReader 方法 149 MySqlDataAdapter adapter = new MySqlDataAdapter(); 150 adapter.SelectCommand = cmd; 151 DataSet ds = new DataSet(); 152 153 adapter.Fill(ds); 154 //清除参数 155 cmd.Parameters.Clear(); 156 conn.Close(); 157 return ds; 158 } 159 catch (Exception e) 160 { 161 throw e; 162 } 163 } 164 165 166 167 /// <summary> 168 /// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列 169 /// </summary> 170 /// <remarks> 171 ///例如: 172 /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); 173 /// </remarks> 174 ///<param name="connectionString">一个有效的连接字符串</param> 175 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 176 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 177 /// <param name="commandParameters">执行命令所用参数的集合</param> 178 /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns> 179 public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 180 { 181 MySqlCommand cmd = new MySqlCommand(); 182 183 using (MySqlConnection connection = new MySqlConnection(connectionString)) 184 { 185 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 186 object val = cmd.ExecuteScalar(); 187 cmd.Parameters.Clear(); 188 return val; 189 } 190 } 191 192 /// <summary> 193 /// 用指定的数据库连接执行一个命令并返回一个数据集的第一列 194 /// </summary> 195 /// <remarks> 196 /// 例如: 197 /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); 198 /// </remarks> 199 /// <param name="connection">一个存在的数据库连接</param> 200 /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param> 201 /// <param name="cmdText">存储过程名称或者sql命令语句</param> 202 /// <param name="commandParameters">执行命令所用参数的集合</param> 203 /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns> 204 public static object ExecuteScalar(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters) 205 { 206 207 MySqlCommand cmd = new MySqlCommand(); 208 209 PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 210 object val = cmd.ExecuteScalar(); 211 cmd.Parameters.Clear(); 212 return val; 213 } 214 215 /// <summary> 216 /// 将参数集合添加到缓存 217 /// </summary> 218 /// <param name="cacheKey">添加到缓存的变量</param> 219 /// <param name="commandParameters">一个将要添加到缓存的sql参数集合</param> 220 public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters) 221 { 222 parmCache[cacheKey] = commandParameters; 223 } 224 225 /// <summary> 226 /// 找回缓存参数集合 227 /// </summary> 228 /// <param name="cacheKey">用于找回参数的关键字</param> 229 /// <returns>缓存的参数集合</returns> 230 public static MySqlParameter[] GetCachedParameters(string cacheKey) 231 { 232 MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey]; 233 234 if (cachedParms == null) 235 return null; 236 237 MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length]; 238 239 for (int i = 0, j = cachedParms.Length; i < j; i++) 240 clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone(); 241 242 return clonedParms; 243 } 244 245 /// <summary> 246 /// 准备执行一个命令 247 /// </summary> 248 /// <param name="cmd">sql命令</param> 249 /// <param name="conn">OleDb连接</param> 250 /// <param name="trans">OleDb事务</param> 251 /// <param name="cmdType">命令类型例如 存储过程或者文本</param> 252 /// <param name="cmdText">命令文本,例如:Select * from Products</param> 253 /// <param name="cmdParms">执行命令的参数</param> 254 private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms) 255 { 256 257 if (conn.State != ConnectionState.Open) 258 conn.Open(); 259 260 cmd.Connection = conn; 261 cmd.CommandText = cmdText; 262 263 if (trans != null) 264 cmd.Transaction = trans; 265 266 cmd.CommandType = cmdType; 267 268 if (cmdParms != null) 269 { 270 foreach (MySqlParameter parm in cmdParms) 271 cmd.Parameters.Add(parm); 272 } 273 } 274 275 } 276 }