/* MySql 类 */using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using MySql.Data.MySqlClient;using MySql.Data;using System.Data;namespace DbMysql{ public class CDbMysql { #region 字段设置 ////// 数据库连接-IP地址 /// public string db_host { set; private get; } ////// 数据库连接-用户名 /// public string db_uname { set; private get; } ////// 数据库连接-密码 /// public string db_upswd { set;private get;} ////// 数据库连接-数据库名称 /// public string db_database { set; private get; } ////// 数据库连接-端口 /// public string db_prost { set; private get; } ////// 数据库连接-数据库编码 /// public string db_charset { set; private get; } ////// 数据库连接-连接句柄 /// private MySqlConnection db_header; ////// 连接字符串 /// private string dh_con_string { set; get; } public string DbError { private set; get; } #endregion ////// 构造函数 /// /// 主机IP /// 用户名 /// 密码 /// 端口 /// 编码-默认utf8 public CDbMysql(string host, string uname, string upassword,string dbname, string prost, string charset = "utf8") { this.db_host = host; this.db_uname = uname; this.db_upswd = upassword; this.db_database = dbname; this.db_prost = prost; this.db_charset = charset; // User Id=root;Host=localhost;Database=studb;Password=root;Port=3307 this.dh_con_string = string.Format("User Id={0};Host={1};Database={2};Password={3};Port={4}",this.db_uname, this.db_host,this.db_database,this.db_upswd,this.db_prost ); this.DbConnection(); } ////// 连接数据库 /// private void DbConnection(){ this.db_header = new MySqlConnection(this.dh_con_string); } ////// 执行SQL语句 /// /// ///public int ExecuteSql(string QueryString) { try { this.db_header.Open(); using (MySqlCommand comm = new MySqlCommand(QueryString, this.db_header)) { int result = comm.ExecuteNonQuery(); this.DbClose(this.db_header); return result; } } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); return -1; } finally { this.DbClose(this.db_header); } } /// /// 返回DataTable /// /// /// ///public DataTable GetDataTable(string SqlString, string TablName) { try { this.db_header.Open(); MySqlDataAdapter Da = new MySqlDataAdapter(SqlString, this.db_header); DataTable dt = new DataTable(TablName); Da.Fill(dt); return dt; } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); return null; } } /// /// 返回DataReader对象 /// /// ///public MySqlDataReader GetDataReader(string SqlString) { try { this.db_header.Open(); MySqlCommand comm = new MySqlCommand(SqlString, this.db_header); MySqlDataReader dread = comm.ExecuteReader(CommandBehavior.Default); return dread; } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); return null; } } /// /// 获取DataAdapter对象 /// /// ///private MySqlDataAdapter GetDataAdapter(string SqlString) { try { this.db_header.Open(); MySqlDataAdapter dadapter = new MySqlDataAdapter(SqlString, this.db_header); return dadapter; } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); return null; } } /// /// 返回DataSet对象 /// /// /// ///public DataSet GetDataSet(string SqlString,string TableName) { try { this.db_header.Open(); MySqlDataAdapter Da = this.GetDataAdapter(SqlString); DataSet ds = new DataSet(); Da.Fill(ds, TableName); return ds; } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); return null; } } /// /// 获取一条数据 /// /// ///public string GetOne(string SqlString) { string result = null; try { this.db_header.Open(); MySqlCommand comm = new MySqlCommand(SqlString, this.db_header); MySqlDataReader dr = comm.ExecuteReader(); if (dr.Read()) { result = dr[0].ToString(); dr.Close(); } else { result = null ; dr.Close(); } } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); } return result; } /// /// 连接测试 /// ///public bool TestConn() { try { this.db_header.Open(); return true; } catch (MySqlException ex) { this.DbError = ex.Message.ToString(); return false; } } /// /// 关闭数据库句柄 /// public void DbClose(MySqlConnection DbHeader) { if (DbHeader != null) { this.db_header.Close(); this.db_header.Dispose(); }; GC.Collect(); } }}