C# MySql 操作类

/*
    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 字段设置
        /// <summary>
        /// 数据库连接-IP地址
        /// </summary>
        public string db_host { set; private get; }
        /// <summary>
        /// 数据库连接-用户名
        /// </summary>
        public string db_uname { set; private get; }
        /// <summary>
        /// 数据库连接-密码
        /// </summary>
        public string db_upswd { set;private get;}
        /// <summary>
        /// 数据库连接-数据库名称
        /// </summary>
        public string db_database { set; private get; }
        /// <summary>
        /// 数据库连接-端口
        /// </summary>
        public string db_prost { set; private get; }
        /// <summary>
        /// 数据库连接-数据库编码
        /// </summary>
        public string db_charset { set; private get; }
        /// <summary>
        /// 数据库连接-连接句柄
        /// </summary>
        private MySqlConnection db_header;
        /// <summary>
        /// 连接字符串
        /// </summary>
        private string dh_con_string { set; get; }

        public string DbError { private set; get; }
        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="host">主机IP</param>
        /// <param name="uname">用户名</param>
        /// <param name="upassword">密码</param>
        /// <param name="prost">端口</param>
        /// <param name="charset">编码-默认utf8</param>
        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();
        }
        /// <summary>
        /// 连接数据库
        /// </summary>
        private void DbConnection(){
            this.db_header = new  MySqlConnection(this.dh_con_string);
        }
       /// <summary>
       /// 执行SQL语句
       /// </summary>
       /// <param name="QueryString"></param>
       /// <returns></returns>
        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);
            }
        }
        /// <summary>
        /// 返回DataTable
        /// </summary>
        /// <param name="SqlString"></param>
        /// <param name="TablName"></param>
        /// <returns></returns>
        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;
            }

        }
        /// <summary>
        /// 返回DataReader对象
        /// </summary>
        /// <param name="SqlString"></param>
        /// <returns></returns>
        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;
            }

        }
        /// <summary>
        /// 获取DataAdapter对象
        /// </summary>
        /// <param name="SqlString"></param>
        /// <returns></returns>
        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;
            }

        }
        /// <summary>
        /// 返回DataSet对象
        /// </summary>
        /// <param name="SqlString"></param>
        /// <param name="TableName"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="SqlString"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 连接测试
        /// </summary>
        /// <returns></returns>
        public bool TestConn() {
            try {
                this.db_header.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                this.DbError = ex.Message.ToString();
                return false;
            }
        }
        /// <summary>
        /// 关闭数据库句柄
        /// </summary>
        public void DbClose(MySqlConnection DbHeader) {
            if (DbHeader != null) {
                this.db_header.Close();
                this.db_header.Dispose();
            };

            GC.Collect();
        }

    }
}
时间: 2024-10-14 10:05:57

C# MySql 操作类的相关文章

ecshop的Mysql操作类

摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MYSQL 公用类库 * ============================================================================ * * 版权所有 2005-2012 上海商派网络科技有限公司,并保留所有权利. * 网站地址: http://www.ecsho

Python MySql 操作类

Python 2.7 暂时只用到这么多,以后用到其他的再补充. # -*- coding:utf-8 -*- import MySQLdb import time ''' · MySQL 操作类 · V1.0 ''' class MySQLClass(object): def __init__(self,host,user,password,charset="utf8"): super(MySQLClass, self).__init__() self.host=host self.u

php编写mysql操作类

文件:mysql.class.php <?php error_reporting(E_ALL ^ E_DEPRECATED); /* mysql操作类 * 1.连接数据库 * 2.选择需要操作的库 * 3.设置操作的编码 * 4.增删改查 * 查询分两种情况: * 1.查询一条数据:执行一次mysql_fetch_assoc * 2.查询多跳数据:执行多次mysql_fetch_assoc * 5.关闭数据库 */ class mysql { public $hostname; //mysql主

php 的mysql操作类

亲自测试,网上其他版本没法用,有很多错误,这是本人亲自测试用的,绝对增删改查都可以. <?php /** * Created by Netbeans. * User: Lugo * Date: 16-7-14 * Version: 1.0.0 * Time: 上午10:50 */ class MysqlHelper { const HOST="localhost"; const DATABASE = "demo"; const ENCODING = "

MySQL操作类的封装(PHP)

<?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操作有误,错误原因为:".$error);//die有两种作用 输出 和 终止 相当于 echo 和 exit 的组合 } /** * 连接数据库 * * @param string $dbhost 主机名 * @param string $dbuser 用户名 * @param string $db

php for mysql 操作类

1 <?php 2 defined('TOKEN') or die('Hacker intrusion'); 3 class dbm { 4 private static $_instance = false; //连接实例 5 private $linkid = ''; //当前的连接资源 6 private $dbname = ''; //数据库名 7 private $tbpre = ''; //表前缀 8 public $sql = array(); //所有执行过的sql 9 publ

PHP封装的一个单例模式Mysql操作类

掌握满足单例模式的必要条件----三私一公. ①私有的构造方法-为了防止在类外使用new关键字实例化对象. ②私有的成员属性-为了防止在类外引入这个存放对象的属性. ③私有的克隆方法-为了防止在类外通过clone成生另一个对象. ④公有的静态方法-为了让用户进行实例化对象的操作. DB类及相关表操作: class mysql_db{     //1.私有的静态属性   private static $dbcon = false;     //2.私有的构造方法   private functio

PHP值mysql操作类

<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/6/27 * Time: 18:55 */ Class Mysqls{ private $table; //表 private $opt; public function __construct($host,$user,$pwd,$name,$table_names) { $this->db=new mysqli($host,$user,$pwd,$name)

php封装mysql操作类

<?php class mysql{ private $host; private $name; private $pass; private $table; private $ut; function __construct($host,$name,$pass,$table,$ut){ $this->host=$host; $this->name=$name; $this->pass=$pass; $this->table=$table; $this->ut=$ut;