开发者

Connecting to a mySQL database using asp.net

So after some tries I figured out I needed a driver for this. I've installed the Components from the links below. But I still can't find any SQL references when I try to add them? I'm wondering if anyone would know the reason for this? I just started with asp.net. I've found several other questions regarding code for connecting but I can't find anyo开发者_如何学Gone who've had trouble with the Connector/components before?

MYSQL connector http://dev.mysql.com/downloads/connector/net/5.0.html

Microsoft Data Access Components (MDAC) 2.8 http://www.microsoft.com/downloads/details.aspx?FamilyID=6c050fe3-c795-4b7d-b037-185d0506396c&displaylang=en

I am using Visual Studio.


Right click on References, then select "Add Reference". Browse and select Mysql.Data.dll.

The dll should be found in the installation directory of the connector you downloaded.

Connecting to a mySQL database using asp.net

Finally, in code:

using MySql.Data.MySqlClient;

And, sample connection:

MySqlConnection connection = new MySqlConnection("Database=database_name;Data Source=server_domain_or_ip;User Id=mysql_user;Password=mysql_password");
connection.Open();

MySqlCommand command =  connection.CreateCommand();
command.CommandText = "select * from mytable";
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
  //reader.GetString(0)
  //reader["column_name"].ToString()
}
reader.Close();


You can browse to the DLL and add a reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜