Net method to connect to the mysql database: first reference the MySQL connector/Net component; then add a reference to MySQL.Data.dll in the ASP.NET project reference; finally use the ConnectMySql code to complete the connection to the database. .
Recommended: "mysql video tutorial"
ASP.NET default The database is MS SQL Server, Microsoft's database product. In fact, if cost factors are not taken into account, Windows Server IIS MS SQL Server ASP.NET is the best combination for website application development. However, since these Microsoft products require charges, when considering cost factors, open source products are often chosen for other products while the development environment ASP.NET remains unchanged.
MySQL is an excellent open source database. Now let’s talk about how to use ASP.NET to connect to the MySQL database (under Windows environment).
1. Install the MySQL database system. As of the time of writing this article, the MySQL version is 5.6.16. You can visit the official website http://dev.mysql.com/downloads/mysql/ Download and install, pay attention to select the Microsoft Windows platform for installation;
2. Reference the MySQL connector/Net component, which is in the MySQL installation directory. For example, the directory on my computer is:
C:\Program Files (x86)\MySQL\Connector NET 6.7.4\Assemblies\v4.5\MySQL.Data.dll
Or go to the official website to download the latest version of MySQL connector/Net component from http://dev.mysql.com/downloads/connector/net/
It should be noted that if it is installed normally After the MySQL database is updated, the MySQL Connector/Net component will be updated, and the problem of different versions will occur. This requires commenting out the statement about MySQL referencing the MySQL Connector/Net component version in machine.config in ASP.NET;
The way to reference the MySQL connector/Net component is to add a reference to MySQL.Data.dll in the ASP.NET project reference, as shown in the following figure:
3-1. Use the following code to complete the connection to the database:
using MySql.Data.MySqlClient;namespace ConnectMySql { class Class1 { public MySqlDataReader GetData() { string connection = "server=localhost;user id=root;password=123456;database=ABC; pooling=true;"; MySqlConnection conn = new MySqlConnection(connection); string sqlQuery = "SELECT * FROM Article"; MySqlCommand comm = new MySqlCommand(sqlQuery, conn); conn.Open(); MySqlDataReader dr = comm.ExecuteReader(); conn.Close(); return dr; } } }
Note two points on the code: First, use using MySql.Data.MySqlClient; The second is to write the connection string connection. Of course, this connection string does not need to be written in specific code as above, but it is recommended to write it in the configuration file in the root directory of the ASP.NET application. The
3-2. Configure the connection string in web.config to complete the connection to the MySQL database (recommended, alternative method 3-1)
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Sanlogic.MSTWebsite-20140219093639;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Sanlogic.MSTWebsite-20140219093639.mdf" providerName="System.Data.SqlClient" /> <add name="DBConnection" connectionString="server=localhost;user id=root;password=123456;database=ABC; pooling=true;" providerName="MySql.Data.MySqlClient" /> </connectionStrings>
where DBConnection is the connection to MySQL. If the connection string is configured like this, you can use the following code to take it out (note that there is still the using MySql.Data.MySqlClient; statement):
public static MySqlConnection CreateConn() { string _conn = WebConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; MySqlConnection conn = new MySqlConnection(_conn); return conn; }
Use this method to take out the connection string and establish a connection to the database. In the future, you will directly call this method to create a connection to the MySQL database, and use ADO.NET to complete the task.
The above is the detailed content of .net how to connect to mysql database. For more information, please follow other related articles on the PHP Chinese website!