In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the method of .net connection to mysql database, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
How net connects to the mysql database: first reference the MySQL connector/Net component, then add a reference to the local MySQL.Data.dll in the ASP.NET project reference, and finally use ConnectMySql code to complete the connection to the database.
The default database for ASP.NET is MS SQL Server, Microsoft's database product. In fact, Windows Server + IIS + MS SQL Server + ASP.NET is the best combination for website application development, regardless of cost. However, because these Microsoft products are charged, when considering the cost factor, other products often choose open source products when the development environment ASP.NET remains the same.
MySQL is an excellent open source database, now let's talk about how to use ASP.NET to connect to MySQL database (in Windows environment).
1. Install the MySQL database system. As of the writing time of this article, the version of MySQL is 5.6.16. You can go to the official website http://dev.mysql.com/downloads/mysql/ to download and install. Pay attention to the installation and select the Microsoft Windows platform.
2. Refer to the MySQL connector/Net component, which is located in the installation directory of MySQL. For example, the directory on your computer is:
C:\ Program Files (x86)\ MySQL\ Connector NET 6.7.4\ Assemblies\ v4.5\ MySQL.Data.dll
Or download the latest version of the MySQL connector/Net component of http://dev.mysql.com/downloads/connector/net/ from the official website.
It should be noted that if the MySQL Connector/Net component is updated after the normal installation of the MySQL database, there will be a problem of different versions. It is necessary to comment out the statements in machine.config in ASP.NET about MySQL referencing the MySQL Connector/Net component version.
The way to reference the MySQL connector/Net component is to add a reference to the local MySQL.Data.dll in the ASP.NET project reference, as shown in the following figure:
3-1. Complete the connection to the database using the following code:
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;}
There are two points to note in the code: one is to use using MySql.Data.MySqlClient; and the other is to write the connection string connection. Of course, this connection string can not be written in the specific code as above, but is recommended to be written in the section of the configuration file web.config in the root directory of the ASP.NET application, as follows:
3-2. Configure the connection string in web.config to complete the connection to the MySQL database (recommended, alternative 3-1)
Where DBConnection is the connection to MySQL. If you configure the connection string like this, you can use the following code to take it out (note that there is still a 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, call this method directly to create a connection to the MySQL database, and use ADO.NET to complete the task.
These are all the contents of the. net method of connecting to the mysql database. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.