1. 程式人生 > >c# 連線 mysql 以及實現簡易增刪改查

c# 連線 mysql 以及實現簡易增刪改查

1.首先下載連線mysql需要的dll,任意門:http://pan.baidu.com/share/link?shareid=1024071329&uk=2442594409

2.本人選用的時2.0的;

3.然後在工程新增引用如下:


相關類:

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Emergency.DBClient
{
    public class MySQLconnection
    {
        /// <summary>
        /// MySqlConnection連線物件
        /// </summary>
        private MySqlConnection connection;
        /// <summary>
        /// 伺服器地址
        /// </summary>
        private string server;
        /// <summary>
        /// 資料庫例項名稱
        /// </summary>
        private string database;     
        /// <summary>
        /// 使用者名稱
        /// </summary>  
        private string uid;
        /// <summary>
        /// 密碼
        /// </summary>
        private string password;
        /// <summary>
        /// 埠號
        /// </summary>
        private string port;

        public MySqlConnection getInstance() {
            return connection;
        }

        /// <summary>
        /// 初始化mysql連線
        /// </summary>
        /// <param name="server">伺服器地址</param>
        /// <param name="database">資料庫例項</param>
        /// <param name="uid">使用者名稱稱</param>
        /// <param name="password">密碼</param>
        public void Initialize(string server, string database, string uid, string password)
        {
            this.server = server;
            this.uid = uid;
            this.password = password;
            this.database = database;
            //string connectionString = "Data Source=" + server + ";" + "port=" + port + ";" + "Database=" + database + ";" + "User Id=" + uid + ";" + "Password=" + password + ";" + "CharSet = utf8"; ;
            string connectionString = "server=" + server + ";user id=" + uid + ";password=" + password + ";database=" + database;
            connection = new MySqlConnection(connectionString);
        }
        /// <summary>
        /// 開啟資料庫連線
        /// </summary>
        /// <returns>是否成功</returns>
        public bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                //When handling errors, you can your application's response based on the error number.
                //The two most common error numbers when connecting are as follows:
                //0: Cannot connect to server.
                //1045: Invalid user name and/or password.
                switch (ex.Number)
                {
                    case 0:
                        Console.Write("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        Console.Write("Invalid username/password, please try again");
                        break;
                }
                return false;
                
            }
        }

       /// <summary>
       /// 關閉資料庫連線
       /// </summary>
       /// <returns></returns>
        public bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        public MySqlDataAdapter GetAdapter(string SQL)
        {
            MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
            return Da;
        }
        /// <summary>
        /// 構建SQL控制代碼
        /// </summary>
        /// <param name="SQL">SQL語句</param>
        /// <returns></returns>
        public MySqlCommand CreateCmd(string SQL)
        {
            MySqlCommand Cmd = new MySqlCommand(SQL, connection);
            return Cmd;
        }
        /// <summary>
        /// 根據SQL獲取DataTable資料表
        /// </summary>
        /// <param name="SQL">查詢語句</param>
        /// <param name="Table_name">返回表的表名</param>
        /// <returns></returns>
        public DataTable GetDataTable(string SQL, string Table_name)
        {
            MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
            DataTable dt = new DataTable(Table_name);
            Da.Fill(dt);
            return dt;
        }

        /// <summary>
        ///  執行MySql語句返回 MySqlDataReader物件
        /// </summary>
        /// <param name="查詢語句"></param>
        /// <returns>MySqlDataReader物件</returns>
        public MySqlDataReader GetReader(string SQL)
        {
            MySqlCommand Cmd = new MySqlCommand(SQL, connection);
            MySqlDataReader Dr;
            try
            {
                Dr = Cmd.ExecuteReader(CommandBehavior.Default);
            }
            catch
            {
                throw new Exception(SQL);
            }
            return Dr;
        }

        /// <summary>
        /// 執行MySql語句,返回DataSet物件
        /// </summary>
        /// <param name="SQL">查詢語句</param>
        /// <param name="Ds">待填充的DataSet物件</param>
        /// <param name="tablename">表名</param>
        /// <returns></returns>
        public DataSet Get_DataSet(string SQL,DataSet Ds, string tablename)
        {
            MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
            try
            {
                Da.Fill(Ds, tablename);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            return Ds;
        }
        /// <summary>
        /// 執行MySql語句,返回DataSet物件,將資料進行了分頁
        /// </summary>
        /// <param name="SQL">查詢語句</param>
        /// <param name="Ds">待填充的DataSet物件</param>
        /// <param name="StartIndex">開始項</param>
        /// <param name="PageSize">每頁資料條數</param>
        /// <param name="tablename">表名</param>
        /// <returns></returns>
        public DataSet GetDataSet(string SQL, DataSet Ds, int StartIndex, int PageSize, string tablename)
        {
            MySqlDataAdapter Da = new MySqlDataAdapter(SQL, connection);
            try
            {
                Da.Fill(Ds, StartIndex, PageSize, tablename);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            return Ds;
        }

        /// <summary>
        /// 新增資料
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public void getInsert(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                String message = ex.Message;
                Console.WriteLine("插入資料失敗了!" + message);
            }

        }
        /// <summary>
        /// 修改資料
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public static void getUpdate(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {

                String message = ex.Message;
                Console.WriteLine("修改資料失敗了!" + message);
            }
        }
        /// <summary>
        /// 刪除資料
        /// </summary>
        /// <param name="mySqlCommand"></param>
        public static void getDel(MySqlCommand mySqlCommand)
        {
            try
            {
                mySqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                String message = ex.Message;
                Console.WriteLine("刪除資料失敗了!" + message);
            }
        }


    }
}

</pre><p></p><p>連線測試:</p><p><pre name="code" class="csharp">
</pre><pre name="code" class="csharp">protected void Application_Start(object sender, EventArgs e)
        {
            string server = "localhost";  //伺服器
            string database = "db";  //資料庫例項
            string uid = "uid";//使用者名稱
            string password = "pwd"; //密碼
            MySQLconnection mysqlconn = new MySQLconnection();
            mysqlconn.Initialize(server,database,uid,password);
            mysqlconn.OpenConnection();
            
        }

原始碼檔案:http://download.csdn.net/detail/dujiajiyiyi/9571610