C#連接mysql的方法【基于vs2010】
本文實例講述了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 it
MySql.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 reference
MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand();
//define the connection used by the command object
msqlCommand.Connection = this.msqlConnection;
//define the command text
msqlCommand.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();
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
Unity?UGUI的MaskableGraphic可遮罩圖形組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的MaskableGraphic可遮罩圖形組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C#開發(fā)Windows UWP系列之對話框MessageDialog和ContentDialog
這篇文章介紹了C#開發(fā)Windows UWP系列之對話框MessageDialog和ContentDialog,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實現(xiàn)方法,結(jié)合實例形式較為詳細(xì)的分析了順序表的定義、原理與具體實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11

