Home >Database >Mysql Tutorial >How to Connect to a MySQL Database in ASP.NET Instead of Using SqlConnection?
This article aims to resolve the error encountered when attempting to connect to a MySQL database using SqlConnection in ASP.NET.
The error message indicates that SqlConnection is only supported for connecting to SQL Server databases. When trying to connect to MySQL, it will fail with an exception.
To connect to a MySQL database from ASP.NET, you need to use MySqlConnection instead of SqlConnection. MySqlConnection is designed specifically for connecting to MySQL databases, while SqlConnection is only for SQL Server connections.
Here's how to use MySqlConnection in your code:
MySqlConnection connection = new MySqlConnection("server=127.0.0.1;uid=root;pwd=1234;database=gis_server");
This creates a new MySqlConnection object with the connection details for the MySQL database. You can then use this object to execute queries and interact with the database.
Remember that you will also need to use the MySqlCommand object instead of the SqlCommand object when executing queries.
For more information on using MySQL in ASP.NET, you can refer to the following resource:
The above is the detailed content of How to Connect to a MySQL Database in ASP.NET Instead of Using SqlConnection?. For more information, please follow other related articles on the PHP Chinese website!