How to connect. Net to MySQL database

Source: Internet
Author: User

. Net connection MySQL database driver package MySQL. Data. dll

It encapsulates the ADO. Net operations of all MySQL Databases in almost the same way as SQL Server (for example, sqlcommand in SQL Server is called mysqlcommand in MySQL ).

 

Use the following method:

1) install the database driver package MySQL. Data. dll (mysql-connector-net-5.0.8.1.exe ).

2) introduce the mysql. Data. dll package in your own solution (project. The steps are as follows:

A) Right-click the reference in solution.

B) on the displayed page, click Add reference.

 

C) Click browsing options in the pop-up dialog box.

D) Find the mysql. Data. dll package you just installed in the search range text box.

Note: The default path of MySQL. Data. dll is:

C: \ Program Files \ mysql connector net 5.0.8.1 \ binaries \. NET 2.0

 

3) Write a connection to the MySQL databaseCode(Example :)


Using mysql. Data. mysqlclient; <--- this space must be introduced

// The server address of the data source database

// Database Name of the database to be connected

// User ID: User Name of the database

// Password Database Password

// Port Database port number (3306 can be omitted)

 

 

// Connect to the database

Try
{

Myconn = new mysqlconnection ("Data Source = 192.168.1.100;" + "Initial catalog = dbname;" + "User ID = root; Pwd = root ");
Myconn. open ();
MessageBox. Show ("database connected ");
}
Catch (mysqlexception myerror)
{
MessageBox. Show ("MySQL connection error:" + myerror. Message );

}

 

 

// Query the database

 

Mysqlcommand com;
Mysqldatareader Dr;
COM = new mysqlcommand ("select * From db_table", myconn );
Dr = com. executereader ();
While (dr. Read ())

Textbox1.appendtext ("** =" + Dr ["*"] + "" + environment. newline );
Dr. Close ();

}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.