博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# MySql 操作类
阅读量:4544 次
发布时间:2019-06-08

本文共 6538 字,大约阅读时间需要 21 分钟。

/*    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(); } }}

 

转载于:https://www.cnblogs.com/flay/p/4234952.html

你可能感兴趣的文章
mysql select
查看>>
Nginx学习——Nginx进程间的通信
查看>>
5.2 上午
查看>>
[LintCode] Sort Integers 整数排序
查看>>
71. Simplify Path
查看>>
CodeForces - 123E Maze
查看>>
ZOJ 1709 Oil Deposits(dfs,连通块个数)
查看>>
安卓开源项目周报0308
查看>>
记可敬可佩的老车同志
查看>>
Maven in 5 Minutes(Windows)
查看>>
常用前端开发工具合集
查看>>
T-SQL:SQL Server-数据开发(经典)
查看>>
IOS 截取字符串
查看>>
Apache 如何设置默认首页文档?
查看>>
NTP服务
查看>>
Hihocoder 二分图一·二分图判定
查看>>
js点击后将文字复制到剪贴板,将图片复制到剪贴板
查看>>
CSS选择符
查看>>
[Win32]一个调试器的实现(六)显示源代码
查看>>
play!:安装以及创建项目
查看>>