C#连接mysql (vs2010)

WBOY
WBOYオリジナル
2016-06-07 15:46:391090ブラウズ

在vs2010中 工具-数据库连接 里要想连接到mysql数据库,需要安装这样一个东西: MySql Connector;1.首先安装 connector 。下载地址:(http://www.mysql.com/downloads/connector/net/)2.现在就可以了,打开vs2010试试吧! 安装完成后找到安装目录下(C:\Pr

在vs2010中  工具->数据库连接   里要想连接到mysql数据库,需要安装这样一个东西: MySql Connector;1.首先安装 connector 。下载地址:(http://www.mysql.com/downloads/connector/net/)2.现在就可以了,打开vs2010试试吧!

 

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

 

现在一切就OK了!

如果想在工程c#中连接mysql,需如下code:

<span>//define the connection reference and initialize it</span>
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlConnection</span> msqlConnection <span><strong>=</strong></span> <span><strong>null</strong></span><span><strong>;</strong></span>
 msqlConnection <span><strong>=</strong></span> <span><strong>new</strong></span> MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlConnection</span><span><strong>(</strong></span>“<span><strong>server</strong></span><span><strong>=</strong></span>localhost<span><strong>;</strong></span>user id<span><strong>=</strong></span>UserName<span><strong>;</strong></span>Password<span><strong>=</strong></span>UserPassword<span><strong>;</strong></span>database<span><strong>=</strong></span>DatabaseName<span><strong>;</strong></span>persist security info<span><strong>=</strong></span><span><strong>False</strong></span>”<span><strong>)</strong></span><span><strong>;</strong></span>
 <span>//define the command reference</span>
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlCommand</span> msqlCommand <span><strong>=</strong></span> <span><strong>new</strong></span> MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlCommand</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
 <span>//define the connection used by the command object</span>
 msqlCommand.<span>Connection</span> <span><strong>=</strong></span> this.<span>msqlConnection</span><span><strong>;</strong></span>
 <span>//define the command text</span>
 msqlCommand.<span>CommandText</span> <span><strong>=</strong></span> <span>"SELECT * FROM TestTable;"</span><span><strong>;</strong></span>
 try
<span><strong>{</strong></span>
 <span>//open the connection</span>
 this.<span>msqlConnection</span>.<span>Open</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
 <span>//use a DataReader to process each record</span>
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlDataReader</span> msqlReader <span><strong>=</strong></span> msqlCommand.<span>ExecuteReader</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
<span><strong> while</strong></span> <span><strong>(</strong></span>msqlReader.<span>Read</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>)</strong></span>
 <span><strong>{</strong></span>
 <span>//do something with each record</span>
 <span><strong>}</strong></span>
<span><strong>}</strong></span>
catch <span><strong>(</strong></span>Exception er<span><strong>)</strong></span>
<span><strong>{</strong></span>
 <span>//do something with the exception</span>
<span><strong>}</strong></span>
finally
<span><strong>{</strong></span>
<span> //always close the connection</span>
 this.<span>msqlConnection</span>.<span>Close</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
<span><strong>}</strong></span>
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。