首先在官網下載,mysql-connect-net,用於使用mysql的驅動程序,我在下載mysql-connect-net.msi. installer后,執行安裝程序的時候一直無法安裝成功,最簡單的方法是直接下載.zip文件后解壓,無須安裝。
官網地址:http://dev.mysql.com/downloads/file/?id=463757
解壓文件后,
出現了好幾個文件夾,其中有v4和v4.5兩個文件夾,對應vs的不同版本
VS2010使用V4.0下的dll文件
VS2012/2013/2015使用v4.5下的dll文件
其中有一個幫助手冊十分有用:
Documentation文件夾下的ConnectorNET.chm中包含了連接mysql數據庫的API。
MySqlConnection類用來連接數據庫
Constructor:
構造函數
MySqlConnection(String) |
Initializes a new instance of theMySqlConnection class when given a string containing the connection string.
|
打開數據庫
Open |
Opens a database connection with the property settings specified by the ConnectionString.
(Overrides DbConnection.Open().) |
關閉數據庫
Close |
Closes the connection to the database. This is the preferred method of closing any open connection.
(Overrides DbConnection.Close().) |
請將項目文件中的“AutoGenerateBindingRedirects”屬性設置為 true
這個可以找到vs該工程的文件夾下的csproj文件中增加<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
警告即可消除。
也可以參考該網址http://www.cnblogs.com/zoro-zero/p/5867320.html解決問題
constructor:
MySqlCommand(String, MySqlConnection) |
Initializes a new instance of theMySqlCommand class with the text of the query and aMySqlConnection.
|
MySqlCommand(String, MySqlConnection, MySqlTransaction) |
Initializes a new instance of theMySqlCommand class with the text of the query, aMySqlConnection, and theMySqlTransaction.
|
執行sql語句
ExecuteNonQuery |
Executes a SQL statement against the connection and returns the number of rows affected.
(Overrides DbCommand.ExecuteNonQuery().) |
設置或返回sql語句
CommandText |
Gets or sets the SQL statement to execute at the data source.
(Overrides DbCommand.CommandText.) |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //////////////////////////////////////
- using MySql.Data;
- using MySql.Data.MySqlClient;
- //建議使用mysqlClient模式,如果連接的數據庫是mysql的話
- //在C#中,如果想連接數據庫的話,需要使用Connection連接對象。同樣,不同的連接模式下,所使用的連接對象也不同
- //還有三種連接方式
- //(1)System.Data.SqlClient模式,使用sqlServer數據庫比較好
- //(2) System.Data.OleDb模式
- //(3) System.Data.Odbc模式
- //<1>如果使用MsqlClient模式的話,其基本連接字符串和連接對象如下:
- //連接字符串:string connectString = "server=localhost;User Id=root;password=;Database=testDB";
- //屬性server是指數據庫所在的機器(服務器)的IP地址,如果使用當前機器(本地機器)的話,也就是使用自己電腦上的數據庫的話,可以使用"localhost"或者"127.0.0.1",如果使用其它機器上的數據庫的話,使用那台機器的IP地址。
- //database指的數據庫的名字。
- //Id代表連接數據庫的用戶名
- //password代表連接數據庫的密碼,如果密碼為空的話不需要填寫,這樣寫"password="即可。
- namespace CsharpMysql
- {
- class Program
- {
- static void Main(string[] args)
- {
- string constructorString = "server=localhost;User Id=root;password=;Database=test";
- MySqlConnection myConnnect = new MySqlConnection(constructorString);
- myConnnect.Open();
- MySqlCommand myCmd = new MySqlCommand("insert into user(name,year) values('jjj',22)", myConnnect);
- Console.WriteLine(myCmd.CommandText);
- if (myCmd.ExecuteNonQuery() > 0)
- {
- Console.WriteLine("數據插入成功!");
- }
- myCmd.CommandText = "insert into user(name,year) values('jjj4',22)";
- Console.WriteLine(myCmd.CommandText);
- if (myCmd.ExecuteNonQuery() > 0)
- {
- Console.WriteLine("數據插入成功!");
- }
- myCmd.CommandText = "delete from user";
- Console.WriteLine(myCmd.CommandText);
- if (myCmd.ExecuteNonQuery() > 0)
- {
- Console.WriteLine("user表類型數據全部刪除成功!");
- }
- myCmd.Dispose();
- myConnnect.Close();
- }
- }
- }<pre name="code" class="csharp">
參考:
http://blog.csdn.net/cfl20121314/article/details/27106819
http://blog.csdn.net/zhanghaoliangdehao/article/details/7372550
http://blog.csdn.net/apache6/article/details/2778878