首页 > 代码库 > MySqlHelper
MySqlHelper
using MySql.Data.MySqlClient; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ADONETTest2 { class MySqlHelper { private static readonly string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; public static MySqlConnection CreateConnection() { //using (MySqlConnection conn = new MySqlConnection(connstr)) MySqlConnection conn = new MySqlConnection(connstr); conn.Open(); return conn; } public static int ExecuteNonQuery(MySqlConnection conn, string sql, params MySqlParameter[] parameters) { using (MySqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; /* foreach (MySqlParameter p in parameters) { cmd.Parameters.Add(p); }*/ cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } public static int ExecuteNonQuery(string sql, params MySqlParameter[] parameters) { using (MySqlConnection conn = CreateConnection()) { return ExecuteNonQuery(conn, sql, parameters); } } public static object ExecuteScalar(MySqlConnection conn, string sql, params MySqlParameter[] parameters) { using (MySqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } public static object ExecuteScalar(string sql, params MySqlParameter[] parameters) { using (MySqlConnection conn = CreateConnection()) { return ExecuteScalar(conn, sql, parameters); } } public static DataTable ExecuteQuery(MySqlConnection conn, string sql, params MySqlParameter[] parameters) { DataTable table = new DataTable(); using (MySqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = sql; cmd.Parameters.AddRange(parameters); using (MySqlDataReader reader = cmd.ExecuteReader()) { table.Load(reader); } } return table; } public static DataTable ExecuteQuery(string sql, params MySqlParameter[] parameters) { using (MySqlConnection conn = CreateConnection()) { return ExecuteQuery(conn, sql, parameters); } } } }
MySqlHelper
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。