国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#連接mysql的方法【基于vs2010】

2019-10-29 21:12:04
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了C#連接mysql的方法。分享給大家供大家參考,具體如下:

在vs2010中  工具->數(shù)據(jù)庫連接   里要想連接到MySQL數(shù)據(jù)庫,需要安裝這樣一個東西: MySql Connector;

1.首先安裝 connector 。下載地址:(http://www.mysql.com/downloads/connector/net/)

2.現(xiàn)在就可以了,打開vs2010試試吧!

安裝完成后找到安裝目錄下(C:/Program Files/MySQL/MySQL Connector Net 6.4.4/Assemblies/v4.0/MySql.Data.dll,添加引用到工程中.

現(xiàn)在一切就OK了!

如果想在工程c#中連接mysql,需如下代碼:

//define the connection reference and initialize itMySql.Data.MySqlClient.MySqlConnection msqlConnection = null;msqlConnection = new MySql.Data.MySqlClient.MySqlConnection("server=localhost;user id=UserName;Password=UserPassword;database=DatabaseName;persist security info=False");//define the command referenceMySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand();//define the connection used by the command objectmsqlCommand.Connection = this.msqlConnection;//define the command textmsqlCommand.CommandText = "SELECT * FROM TestTable;";try{ //open the connection this.msqlConnection.Open(); //use a DataReader to process each record MySql.Data.MySqlClient.MySqlDataReader msqlReader = msqlCommand.ExecuteReader(); while (msqlReader.Read()) { //do something with each record }}catch (Exception er){ //do something with the exception}finally{ //always close the connection this.msqlConnection.Close();}

希望本文所述對大家C#程序設計有所幫助。


注:相關教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂平县| 岳阳市| 桃园市| 英吉沙县| 伊吾县| 梁河县| 额敏县| 扎鲁特旗| 澄城县| 聂拉木县| 成安县| 鸡泽县| 泽库县| 格尔木市| 罗平县| 东乡族自治县| 鹤峰县| 霍邱县| 宁晋县| 临朐县| 柞水县| 繁峙县| 仁寿县| 筠连县| 三亚市| 江达县| 乌兰察布市| 大姚县| 天气| 河东区| 云林县| 潼关县| 滨州市| 泰来县| 资兴市| 云阳县| 拜城县| 天祝| 嵊泗县| 磴口县| 砀山县|