Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a mySQL database using asp.net

Tags:

mysql

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 anyone 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.

like image 250
Peter Rasmussen Avatar asked Feb 20 '11 00:02

Peter Rasmussen


People also ask

Can ASP.NET connect to MySQL?

To Connect to a MySQL Database Using ASP.NETFind your database's connection strings (Plesk). Note: Change the your password value to your real database password value. Using Microsoft Visual Studio . NET create an ASP.NET Project.

How do I connect to a MySQL database?

To Connect to a MySQL Database Expand the Drivers node from the Database Explorer. Right-click the MySQL (Connector/J driver) and choose Connect Using.... The New Database Connection dialog box is displayed. In the Basic Setting tab, enter the Database's URL <HOST>:<PORT>/<DB> in the corresponding text field.

Can MySQL connect with C#?

Moreover, to connect using MySQL Connector/NET for Windows Application, we can do it through the following code. The above code can be used to integrate MySQL in C# in the native Windows authentication and CRUD operations can then be performed on top of it. We'll learn more about it in our next article.

Does .NET work with MySQL?

Connector/NET includes full support for: Features provided by MySQL Server, up to and including the MySQL 8.0 release series. MySQL as a document store (NoSQL), along with X Protocol connection support to access MySQL data using X Plugin ports.


1 Answers

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.

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();
like image 95
Sarwar Erfan Avatar answered Oct 24 '22 17:10

Sarwar Erfan