IT虾米网

操作mysql的C#类

leader 2018年06月24日 数据库 1050 0
需要mysql.data.dll
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虾米 (左侧二维码扫一扫)欢迎添加!