Chinaunix首页 | 论坛 | 博客
  • 博客访问: 302316
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(32)

分类: C#/.net

2017-01-22 13:37:56


点击(此处)折叠或打开

  1. /*
  2.     MySql 类
  3.  */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using MySql.Data.MySqlClient;
  10. using MySql.Data;
  11. using System.Data;
  12. namespace DbMysql
  13. {
  14.     public class CDbMysql
  15.     {
  16.         #region 字段设置
  17.         ///
  18.         /// 数据库连接-IP地址
  19.         ///
  20.         public string db_host { set; private get; }
  21.         ///
  22.         /// 数据库连接-用户名
  23.         ///
  24.         public string db_uname { set; private get; }
  25.         ///
  26.         /// 数据库连接-密码
  27.         ///
  28.         public string db_upswd { set;private get;}
  29.         ///
  30.         /// 数据库连接-数据库名称
  31.         ///
  32.         public string db_database { set; private get; }
  33.         ///
  34.         /// 数据库连接-端口
  35.         ///
  36.         public string db_prost { set; private get; }
  37.         ///
  38.         /// 数据库连接-数据库编码
  39.         ///
  40.         public string db_charset { set; private get; }
  41.         ///
  42.         /// 数据库连接-连接句柄
  43.         ///
  44.         private MySqlConnection db_header;
  45.         ///
  46.         /// 连接字符串
  47.         ///
  48.         private string dh_con_string { set; get; }
  49.         public string DbError { private set; get; }
  50.         #endregion
  51.         ///
  52.         /// 构造函数
  53.         ///
  54.         /// 主机IP
  55.         /// 用户名
  56.         /// 密码
  57.         /// 端口
  58.         /// 编码-默认utf8
  59.         public CDbMysql(string host, string uname, string upassword,string dbname, string prost, string charset = “utf8″) {
  60.             this.db_host = host;
  61.             this.db_uname = uname;
  62.             this.db_upswd = upassword;
  63.             this.db_database = dbname;
  64.             this.db_prost = prost;
  65.             this.db_charset = charset;
  66.            // User Id=root;Host=localhost;Database=studb;Password=root;Port=3307
  67.             this.dh_con_string = string.Format(“User Id={0};Host={1};Database={2};Password={3};Port={4},this.db_uname,
  68.                                                this.db_host,this.db_database,this.db_upswd,this.db_prost
  69.                                                );
  70.             this.DbConnection();
  71.         }
  72.         ///
  73.         /// 连接数据库
  74.         ///
  75.         private void DbConnection(){
  76.             this.db_header = new MySqlConnection(this.dh_con_string);
  77.         }
  78.        ///
  79.        /// 执行SQL语句
  80.        ///
  81.        ///
  82.        ///
  83.         public int ExecuteSql(string QueryString) {
  84.             try
  85.                 {
  86.                     this.db_header.Open();
  87.                     using (MySqlCommand comm = new MySqlCommand(QueryString, this.db_header)) {
  88.                             int result = comm.ExecuteNonQuery();
  89.                             this.DbClose(this.db_header);
  90.                             return result;
  91.                     }
  92.                 }
  93.             catch (MySqlException ex)
  94.             {
  95.                 this.DbError = ex.Message.ToString();
  96.                 return -1;
  97.             }
  98.             finally
  99.             {
  100.                 this.DbClose(this.db_header);
  101.             }
  102.         }
  103.         ///
  104.         /// 返回DataTable
  105.         ///
  106.         ///
  107.         ///
  108.         ///
  109.         public DataTable GetDataTable(string SqlString, string TablName) {
  110.             try
  111.             {
  112.                 this.db_header.Open();
  113.                 MySqlDataAdapter Da = new MySqlDataAdapter(SqlString, this.db_header);
  114.                 DataTable dt = new DataTable(TablName);
  115.                 Da.Fill(dt);
  116.                 return dt;
  117.             }
  118.             catch (MySqlException ex) {
  119.                 this.DbError = ex.Message.ToString();
  120.                 return null;
  121.             }
  122.         }
  123.         ///
  124.         /// 返回DataReader对象
  125.         ///
  126.         ///
  127.         ///
  128.         public MySqlDataReader GetDataReader(string SqlString) {
  129.             try
  130.             {
  131.                 this.db_header.Open();
  132.                 MySqlCommand comm = new MySqlCommand(SqlString, this.db_header);
  133.                 MySqlDataReader dread = comm.ExecuteReader(CommandBehavior.Default);
  134.                 return dread;
  135.             }
  136.             catch (MySqlException ex)
  137.             {
  138.                 this.DbError = ex.Message.ToString();
  139.                 return null;
  140.             }
  141.         }
  142.         ///
  143.         /// 获取DataAdapter对象
  144.         ///
  145.         ///
  146.         ///
  147.         private MySqlDataAdapter GetDataAdapter(string SqlString) {
  148.             try
  149.             {
  150.                 this.db_header.Open();
  151.                 MySqlDataAdapter dadapter = new MySqlDataAdapter(SqlString, this.db_header);
  152.                 return dadapter;
  153.             }
  154.             catch (MySqlException ex)
  155.             {
  156.                 this.DbError = ex.Message.ToString();
  157.                 return null;
  158.             }
  159.         }
  160.         ///
  161.         /// 返回DataSet对象
  162.         ///
  163.         ///
  164.         ///
  165.         ///
  166.         public DataSet GetDataSet(string SqlString,string TableName) {
  167.             try
  168.             {
  169.                 this.db_header.Open();
  170.                 MySqlDataAdapter Da = this.GetDataAdapter(SqlString);
  171.                 DataSet ds = new DataSet();
  172.                 Da.Fill(ds, TableName);
  173.                 return ds;
  174.             }
  175.             catch (MySqlException ex) {
  176.                 this.DbError = ex.Message.ToString();
  177.                 return null;
  178.             }
  179.         }
  180.         ///
  181.         /// 获取一条数据
  182.         ///
  183.         ///
  184.         ///
  185.         public string GetOne(string SqlString) {
  186.             string result = null;
  187.             try
  188.             {
  189.                 this.db_header.Open();
  190.                 MySqlCommand comm = new MySqlCommand(SqlString, this.db_header);
  191.                 MySqlDataReader dr = comm.ExecuteReader();
  192.                 if (dr.Read())
  193.                 {
  194.                     result = dr[0].ToString();
  195.                     dr.Close();
  196.                 }
  197.                 else
  198.                 {
  199.                     result = null ;
  200.                     dr.Close();
  201.                 }
  202.             }
  203.             catch (MySqlException ex) {
  204.                 this.DbError = ex.Message.ToString();
  205.             }
  206.             return result;
  207.         }
  208.         ///
  209.         /// 连接测试
  210.         ///
  211.         ///
  212.         public bool TestConn() {
  213.             try {
  214.                 this.db_header.Open();
  215.                 return true;
  216.             }
  217.             catch (MySqlException ex)
  218.             {
  219.                 this.DbError = ex.Message.ToString();
  220.                 return false;
  221.             }
  222.         }
  223.         ///
  224.         /// 关闭数据库句柄
  225.         ///
  226.         public void DbClose(MySqlConnection DbHeader) {
  227.             if (DbHeader != null) {
  228.                 this.db_header.Close();
  229.                 this.db_header.Dispose();
  230.             };
  231.             GC.Collect();
  232.         }
  233.     }
  234. }

阅读(883) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~