C#應用MySQLConnectorNet和MySQLDriverCS操作MySQL的辦法。本站提示廣大學習愛好者:(C#應用MySQLConnectorNet和MySQLDriverCS操作MySQL的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用MySQLConnectorNet和MySQLDriverCS操作MySQL的辦法正文
應用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();//封閉銜接 }
履行成果:
數據被勝利讀出。