Rumah > pangkalan data > tutorial mysql > C#连接mysql (vs2010)

C#连接mysql (vs2010)

WBOY
Lepaskan: 2016-06-07 15:46:39
asal
1090 orang telah melayarinya

在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>
Salin selepas log masuk
 MySql.<span>Data</span>.<span>MySqlClient</span>.<span>MySqlConnection</span> msqlConnection <span><strong>=</strong></span> <span><strong>null</strong></span><span><strong>;</strong></span>
Salin selepas log masuk
 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>
Salin selepas log masuk
 <span>//define the command reference</span>
Salin selepas log masuk
 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>
Salin selepas log masuk
 <span>//define the connection used by the command object</span>
Salin selepas log masuk
 msqlCommand.<span>Connection</span> <span><strong>=</strong></span> this.<span>msqlConnection</span><span><strong>;</strong></span>
Salin selepas log masuk
 <span>//define the command text</span>
Salin selepas log masuk
 msqlCommand.<span>CommandText</span> <span><strong>=</strong></span> <span>"SELECT * FROM TestTable;"</span><span><strong>;</strong></span>
Salin selepas log masuk
 try
Salin selepas log masuk
<span><strong>{</strong></span>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
 <span>//open the connection</span>
Salin selepas log masuk
 this.<span>msqlConnection</span>.<span>Open</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
Salin selepas log masuk
 <span>//use a DataReader to process each record</span>
Salin selepas log masuk
 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>
Salin selepas log masuk
<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>
Salin selepas log masuk
 <span><strong>{</strong></span>
Salin selepas log masuk
 <span>//do something with each record</span>
Salin selepas log masuk
 <span><strong>}</strong></span>
Salin selepas log masuk
<span><strong>}</strong></span>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
catch <span><strong>(</strong></span>Exception er<span><strong>)</strong></span>
Salin selepas log masuk
<span><strong>{</strong></span>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
 <span>//do something with the exception</span>
Salin selepas log masuk
<span><strong>}</strong></span>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
finally
Salin selepas log masuk
<span><strong>{</strong></span>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
<span> //always close the connection</span>
Salin selepas log masuk
 this.<span>msqlConnection</span>.<span>Close</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>;</strong></span>
Salin selepas log masuk
<span><strong>}</strong></span>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan