VS2015 How to connect MySQL database graphics

Source: Internet
Author: User

1. Create a new project named MySQL, programming environment Select C #, and then select Windows Forms Application, create a new form to display the datasets queried to the SQL database

2. Drag a button and DataGridView control from the toolbox to the Form1 form, the button is to trigger the connection database to get the dataset, the name of the button is displayed, The DataGridView control is used to display the contents of a DataSet 3. Click the References folder in Solution Explorer and then right-select Add Reference and select Browse to open Mysql.data.dll, which is a dynamic library of C # connection to the MySQL database. It encapsulates a lot of common operations database methods 4. Add using MySql.Data.MySqlClient in the Form1.cs code in Solution Explorer; This is what the code actually references in Mysql.data.dll, with this C # It is easy to operate SQL database 5. Add the following code to the button's Click event

String str = "server=127.0.0.1; User Id=root; Password=123456;database=test; CHARSET=GBK; ";

Mysqlconnection con = new mysqlconnection (str);//instantiation of links

Con. Open ();//Open connection

String strcmd = "SELECT * from user";

Mysqlcommand cmd = new Mysqlcommand (Strcmd, con);

Mysqldataadapter ada = new Mysqldataadapter (cmd);

DataSet ds = new DataSet ();

Ada. Fill (DS);//Query results populate data set

Datagridview1.datasource = ds. Tables[0];

Con. Close ();//Closed connection

6. Use the Navicat software to create a new table user in the database test, and then create a new two fields username and password (columns in the diagram), Navicat software is a GUI tool for MySQL, responsible for creating new tables and database operations such as backups. Intuitive interface to operate 7. After the database has been built, you can execute the project, click the Show button to execute the results as follows, the username and password indicate that the database connection was successful, because there is no data added so the following is empty

VS2015 How to connect MySQL database graphics

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.