IT虾米网

C#连接MySQL的类详解

luoye 2018年06月24日 数据库 604 0

需要mysql.data.dll

c#代码

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 
using System.Data; 
using System.Text.RegularExpressions; 
/// 
using MySql.Data.MySqlClient; 
 
namespace NameSpace 
{ 
    class MysqlConnection 
    { 
        MySqlConnection mysqlConnection; 
        DataSet dataSet; 
        string IP = null; 
        string UserName = "root"; 
        string Password = "root"; 
        string Database = null; 
 
        public MysqlConnection() 
        { 
            try 
            { 
                mysqlConnection = new MySqlConnection("datasource=20.0.0.20;username=root;password=root;database=sysinfo;charset=gb2312"); 
            } 
            catch (MySqlException ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        } 
 
        public MysqlConnection(string IP,string UserName,string Password,string Database) 
        { 
            try 
            { 
                string connectionString = "datasource=" + IP + ";username="+UserName+";password="+Password+";database=" + Database+ ";charset=gb2312" ; 
                mysqlConnection = new MySqlConnection(connectionString); 
            } 
            catch (MySqlException ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
        } 
 
        public string MysqlInfo() 
        { 
            string mysqlInfo = null; 
            try 
            { 
                mysqlConnection.Open(); 
                mysqlInfo += "Connection Opened." + Environment.NewLine; 
                mysqlInfo += "Connection String:" + mysqlConnection.ConnectionString.ToString() + Environment.NewLine; 
                mysqlInfo += "Database:" + mysqlConnection.Database.ToString() + Environment.NewLine; 
                mysqlInfo += "Connection ServerVersion:" + mysqlConnection.ServerVersion.ToString() + Environment.NewLine; 
                mysqlInfo += "Connection State:" + mysqlConnection.State.ToString() + Environment.NewLine; 
            } 
            catch (MySqlException ex) 
            { 
                Console.WriteLine("MySqlException Error:" + ex.ToString()); 
            } 
            finally 
            { 
                mysqlConnection.Close(); 
            } 
            return mysqlInfo; 
        } 
 
        public int MysqlCommand(string MysqlCommand) 
        { 
            try 
            { 
                mysqlConnection.Open(); 
                Console.WriteLine("MysqlConnection Opened."); 
                MySqlCommand mysqlCommand = new MySqlCommand(MysqlCommand, mysqlConnection); 
                return mysqlCommand.ExecuteNonQuery(); 
            } 
            catch (MySqlException ex) 
            { 
                Console.WriteLine("MySqlException Error:" + ex.ToString()); 
                if (Regex.IsMatch(ex.ToString(), "")) 
                { 
                    MessageBox.Show("数据库已经存在唯一键值"); 
                } 
            } 
            finally 
            { 
                mysqlConnection.Close(); 
            } 
            return -1; 
        } 
 
        // 
        public DataView MysqlDataAdapter(string table) 
        { 
            DataView dataView = new DataView(); 
            try 
            { 
                mysqlConnection.Open(); 
                MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter("Select * from " + table, mysqlConnection); 
                dataSet = new DataSet(); 
                mysqlDataAdapter.Fill(dataSet, table); 
                dataView = dataSet.Tables[table].DefaultView; 
            } 
            catch (MySqlException ex) 
            { 
                MessageBox.Show(ex.Message); 
            } 
            finally 
            { 
                mysqlConnection.Close(); 
            } 
            return dataView; 
        } 
    }//end class 
}
发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

MySQL实现统计数据并插入数据的存储过程详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。