.net連接mysql


 

首先在官網下載,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. 
methods:

打開數據庫

 

  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.  
methods:

執行sql語句

 

  ExecuteNonQuery
Executes a SQL statement against the connection and returns the number of rows affected.
(Overrides DbCommand.ExecuteNonQuery().)
property:

設置或返回sql語句

CommandText
Gets or sets the SQL statement to execute at the data source.
(Overrides DbCommand.CommandText.)
通過改變CommandText內容,使MysqlCommand類能夠執行多條sql語句。

 

[csharp]  view plain  copy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. //////////////////////////////////////  
  7. using MySql.Data;  
  8. using MySql.Data.MySqlClient;  
  9. //建議使用mysqlClient模式,如果連接的數據庫是mysql的話  
  10. //在C#中,如果想連接數據庫的話,需要使用Connection連接對象。同樣,不同的連接模式下,所使用的連接對象也不同  
  11. //還有三種連接方式  
  12. //(1)System.Data.SqlClient模式,使用sqlServer數據庫比較好  
  13. //(2) System.Data.OleDb模式  
  14. //(3) System.Data.Odbc模式  
  15. //<1>如果使用MsqlClient模式的話,其基本連接字符串和連接對象如下:  
  16. //連接字符串:string connectString = "server=localhost;User Id=root;password=;Database=testDB";  
  17. //屬性server是指數據庫所在的機器(服務器)的IP地址,如果使用當前機器(本地機器)的話,也就是使用自己電腦上的數據庫的話,可以使用"localhost"或者"127.0.0.1",如果使用其它機器上的數據庫的話,使用那台機器的IP地址。  
  18. //database指的數據庫的名字。  
  19. //Id代表連接數據庫的用戶名  
  20. //password代表連接數據庫的密碼,如果密碼為空的話不需要填寫,這樣寫"password="即可。  
  21. namespace CsharpMysql  
  22. {  
  23.     class Program  
  24.     {  
  25.         static void Main(string[] args)  
  26.         {  
  27.             string constructorString = "server=localhost;User Id=root;password=;Database=test";  
  28.             MySqlConnection myConnnect = new MySqlConnection(constructorString);  
  29.             myConnnect.Open();  
  30.             MySqlCommand myCmd = new MySqlCommand("insert into user(name,year) values('jjj',22)", myConnnect);  
  31.             Console.WriteLine(myCmd.CommandText);  
  32.             if (myCmd.ExecuteNonQuery() > 0)  
  33.             {  
  34.                 Console.WriteLine("數據插入成功!");  
  35.             }           
  36.             myCmd.CommandText = "insert into user(name,year) values('jjj4',22)";  
  37.             Console.WriteLine(myCmd.CommandText);  
  38.             if (myCmd.ExecuteNonQuery() > 0)  
  39.             {  
  40.                 Console.WriteLine("數據插入成功!");  
  41.             }  
  42.             myCmd.CommandText = "delete from user";  
  43.             Console.WriteLine(myCmd.CommandText);  
  44.             if (myCmd.ExecuteNonQuery() > 0)  
  45.             {  
  46.                 Console.WriteLine("user表類型數據全部刪除成功!");  
  47.             }  
  48.             myCmd.Dispose();  
  49.             myConnnect.Close();  
  50.         }  
  51.     }  
  52. }<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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM