C#使用MySQLConnectorNet和MySQLDriverCS操作MySQL的方法_java

來源:互聯網
上載者:User

使用MySQLConnectorNet串連MySQL資料庫
1.首先下載安裝Connector/Net,http://www.mysql.com/downloads/connector/net/
並從安裝目錄中得到所需動態連結程式庫MySql.Data.dll,如果已經有MySql.Data.dll也可以不用安裝。

2.在項目中添加對該動態連結程式庫的引用

3.在mySQL中建一些測試資料如:
我是使用phpAdmin

INSERT INTO stuinfo( first_name, last_name, Birthdate )  VALUES ( 'John', 'Smith', '1990-2-3' )  

static void Main(string[] args)     {       string url = "server=127.0.0.1;user=root;database=student;port=3306;password=root;";//指明要串連的資料庫地址,使用者名稱,資料庫名,連接埠,密碼       MySqlConnection conn = new MySqlConnection(url);//執行個體化串連       conn.Open();//開啟串連        string sta = "select * from stuinfo";//執行一個簡單的語句       MySqlCommand comm = new MySqlCommand(sta, conn);       MySqlDataReader reader = comm.ExecuteReader();//用MySqlDataReader接收執行結果       while (reader.Read())       {         Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2) +" " + reader.GetString(3));//讀出查詢的結果       }       Console.ReadKey();       reader.Close();       conn.Close();//關閉串連     } 

使用MySQLDriverCS串連MySQL資料庫
與使用MySQLConnectorNet大同小異,首先下載安裝MySQLDriverCS後得到動態連結程式庫檔案:
http://sourceforge.net/projects/mysqldrivercs/

將MySQLDriverCS.dll添加到工程的引用中:

還是利用上買呢已經在MySQL資料庫中建立了的測試資料,所以直接編寫C#代碼:

static void Main(string[] args)     {       MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("127.0.0.1","student", "root", "root",3306).AsString);       conn.Open();        MySQLCommand cmd = new MySQLCommand("select * from stuinfo", conn);       DbDataReader reader = cmd.ExecuteReader();       while (reader.Read())       {         Console.WriteLine(reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2) + " " + reader.GetString(3));//讀出查詢的結果         }       Console.ReadKey();       reader.Close();       conn.Close();//關閉串連       } 

執行結果:

資料被成功讀出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.