visual studio 2010 mysql_Visual Studio 2010连接mysql的方法

本文详细介绍了如何在Visual Studio 2010中连接到MySQL数据库,包括下载安装MySql Connector/NET,使用Server Explorer建立连接,以及在C#应用中编写代码进行数据库操作的步骤。
摘要由CSDN通过智能技术生成

In order to connect to a MySql database from VS2010 you need to

download the latest version of the MySql Connector/NET from http://www.mysql.com/downloads/connector/net/

install the connector (if you have an older version you need to remove it from Control Panel -> Add / Remove Programs)

open Visual Studio 2010

open Server Explorer Window (View -> Server Explorer)

7245e16b2b0687060d636fc722a8b472.png --Visual Studio 2010 - Server Explorer window

use Connect to Database button

in the Choose Data Source windows select MySql Database and press Continue

7ad9fdbbe6e59ccd0c290c30bad77b4e.png --VS2010 - MySQL DataSource

in the Add Connection window

set server name: 127.0.0.1 or localhost for MySql server running on local machine or an IP address for a remote server

username and password

if the the above data is correct and the connection can be made, you have the possibility to select the database

d371b43bc33783972aa41683e02e8e9c.png --VS2010 MySQL Connection properties

If you want to connect to a MySql database from a C# application (Windows or Web) you can use the next sequence:

//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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值