c#操作mysql数据库 CRUDclass SqlHelper { //返回链接数据库的字符串 public static string ConnString { get { return "Data Source='localhost';Database='dbdemo';User Id='root';Password='wdf123';charset='utf8'"; } } /// <summary> /// sqlserver 执行通用的添加,修改,删除操作 /// </summary> /// <param name="sql">insert/update/delete语句</param> /// <param name="pars">参数类型的数组</param> /// <returns>int 受影响的条数</returns> public static int mssql_exec(string sql,SqlParameter[] pars) { SqlConnection conn = new SqlConnection(ConnString); try { if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = new SqlCommand(sql, conn); if (pars != null && pars.Length > 0) { foreach (SqlParameter p in pars) { cmd.Parameters.Add(p); } } int count = cmd.ExecuteNonQuery(); return count; }catch(Exception ex){ throw ex; //return 0; } finally { conn.Close(); } } /// <summary> /// sqlserver 执行通用的添加,修改,删除操作 /// </summary> /// <param name="sql">insert/update/delete语句</param> /// <param name="pars">参数类型的数组</param> /// <returns>int 受影响的条数</returns> public static int mysql_exec(string sql, MySqlParameter[] pars) { MySqlConnection conn = new MySqlConnection(ConnString); try { if (conn.State == ConnectionState.Closed) { conn.Open(); } MySqlCommand cmd = new MySqlCommand(sql, conn); if (pars != null && pars.Length > 0) { foreach (MySqlParameter p in pars) { cmd.Parameters.Add(p); } } int count = cmd.ExecuteNonQuery(); return count; } catch (Exception ex) { throw ex; //return 0; } finally { conn.Close(); } }
评论