第一個Net+Mysql的例子,比想象的簡單不少

1.window下安裝mysql,比較簡單,徹底的圖形化界面,不用看文檔一路點擊下來也ok,注意中間幾個configtype選項就能夠。mysql

2.安裝MySql Net的驅動程序程序,安裝完後就是幾個dll,添加到vs項目的引用中就行,MySql.Data。dll,MySql.Data.Entity.Dll。sql

3.在mysql.data命名空間下,能夠對mysql數據庫進行數據操做,原理和語法和sqlserver的,sqlclient徹底同樣,只是命名空間和類不同數據庫

這是我寫mysqlhelper的第一個例子類工具

 public static class MySqlHelper
    {
        public static int ExecuteNonQuery(string connectionString, CommandType commandtype, string commandText)
        {
            return ExecuteNonQuery(connectionString, commandtype, commandText, null);
        }

        public static int ExecuteNonQuery(string connectionString, CommandType commandtype, string commandText, params MySqlParameter[] commandParameters)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new Exception("connectionString exception");
            }
            int result = 0;
            MySqlConnection con = null;
            try
            {
                using (con = new MySqlConnection(connectionString))
                {
                    con.Open();
                    MySqlCommand command = new MySqlCommand(commandText, con);
                    command.CommandType = commandtype;
                    result = command.ExecuteNonQuery();
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
            
        }
    }

4.操做方法,給表name添加一個新紀錄,很簡單,查詢字符串的表達方式和sqlserver徹底一致。sqlserver

           string connectionString = "server=localhost;uid=root;pwd=111111;database=test;";

            string sql = "insert name(name) values('hello')";
            MySqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
            Console.Read();ui

 

另外你們都推薦的mysql圖形管理工具室navicat For Mysql,我感受這個挺簡單,操做很容易上手spa

http://www.connectionstrings.com/mysqlcode

相關文章
相關標籤/搜索