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

C#连接mysql (vs2010)

WBOY
Release: 2016-06-07 15:46:39
Original
1090 people have browsed it

在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>
Copy after login
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlConnection</span> msqlConnection <span><strong>=</strong></span> <span><strong>null</strong></span><span><strong>;</strong></span>
Copy after login
 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>
Copy after login
 <span>//define the command reference</span>
Copy after login
 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>
Copy after login
 <span>//define the connection used by the command object</span>
Copy after login
 msqlCommand.<span>Connection</span> <span><strong>=</strong></span> this.<span>msqlConnection</span><span><strong>;</strong></span>
Copy after login
 <span>//define the command text</span>
Copy after login
 msqlCommand.<span>CommandText</span> <span><strong>=</strong></span> <span>"SELECT * FROM TestTable;"</span><span><strong>;</strong></span>
Copy after login
 try
Copy after login
<span><strong>{</strong></span>
Copy after login
Copy after login
Copy after login
 <span>//open the connection</span>
Copy after login
 this.<span>msqlConnection</span>.<span>Open</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
Copy after login
 <span>//use a DataReader to process each record</span>
Copy after login
 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>
Copy after login
<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>
Copy after login
 <span><strong>{</strong></span>
Copy after login
 <span>//do something with each record</span>
Copy after login
 <span><strong>}</strong></span>
Copy after login
<span><strong>}</strong></span>
Copy after login
Copy after login
Copy after login
catch <span><strong>(</strong></span>Exception er<span><strong>)</strong></span>
Copy after login
<span><strong>{</strong></span>
Copy after login
Copy after login
Copy after login
 <span>//do something with the exception</span>
Copy after login
<span><strong>}</strong></span>
Copy after login
Copy after login
Copy after login
finally
Copy after login
<span><strong>{</strong></span>
Copy after login
Copy after login
Copy after login
<span> //always close the connection</span>
Copy after login
 this.<span>msqlConnection</span>.<span>Close</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
Copy after login
<span><strong>}</strong></span>
Copy after login
Copy after login
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template