C#連接mysql的方法【基于vs2010】
本文實(shí)例講述了C#連接mysql的方法。分享給大家供大家參考,具體如下:
在vs2010中 工具->數(shù)據(jù)庫連接 里要想連接到MySQL數(shù)據(jù)庫,需要安裝這樣一個(gè)東西: 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è)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
上一篇:淺析C# 使用Process調(diào)用外部程序中所遇到的參數(shù)問題
欄 目:C#教程
下一篇:C#生成PDF文件流
本文標(biāo)題:C#連接mysql的方法【基于vs2010】
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5825.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#停止線程的方法
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
 - 01-10C#實(shí)現(xiàn)多線程下載文件的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 


