使用 asp.net 连接到 mySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5054430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:50:34  来源:igfitidea点击:

Connecting to a mySQL database using asp.net

asp.netmysql

提问by Peter Rasmussen

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 anyone who've had trouble with the Connector/components before?

所以经过一些尝试后,我发现我需要一个驱动程序。我已经从下面的链接安装了组件。但是当我尝试添加它们时仍然找不到任何 SQL 引用?我想知道有没有人知道这是什么原因?我刚开始使用asp.net。我发现了其他几个关于连接代码的问题,但我找不到以前遇到过连接器/组件问题的人?

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

MYSQL 连接器 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

Microsoft 数据访问组件 (MDAC) 2.8 http://www.microsoft.com/downloads/details.aspx?FamilyID=6c050fe3-c795-4b7d-b037-185d0506396c&displaylang=en

I am using Visual Studio.

我正在使用 Visual Studio。

回答by Sarwar Erfan

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

右键单击“引用”,然后选择“添加引用”。浏览并选择Mysql.Data.dll

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

dll 应该可以在您下载的连接器的安装目录中找到。

enter image description here

在此处输入图片说明

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

回答by Puneet

You can browse to the DLL and add a reference.

您可以浏览到 DLL 并添加引用。