Home >Database >Mysql Tutorial >C#连接mysql (vs2010)

C#连接mysql (vs2010)

WBOY
WBOYOriginal
2016-06-07 15:46:391090browse

在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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn