C# detailed tutorial on connecting to Mysql database (with Mysql and Navicat included)

        The teaching in the class uses the SqlServer database, because the SqlServer database configuration is difficult to understand, so learn to use Mysql instead of the database connection. (Mysql and Navicat installation instructions are in the compressed package)

        Mysql connection first needs to use the Mysql.Data.dll connection file, which is the Mysql Connector Net file. After downloading, the installation will automatically be in the path of C:\Program Files (x86)\MySQL\MySQL Connector Net 6.9.0 (seemingly ...)

        After that, you need to reference this file in the reference of VS, and then you can perform the connection operation of the database.

         First create a new project, select Windows Forms Application to create a form

 

        Set the form plus components to the style shown in the figure below (you can play freely, as long as you include relevant inputs)

 

        Create the class file of connect.cs (here is the integration class that I set up for the convenience of subsequent information system creation, and you can write less code by calling this class)

        Which needs to add using MySql.Data.MySqlClient at the beginning;

        And define three objects in the class

        public MySqlConnection conn = null;//connection object
        public MySqlCommand comm = null;//statement execution object
        public MySqlDataReader dr = null;//statement execution result data object

        Then use the load() method to encapsulate the connection process, here you need to enter the relevant information of the database by yourself

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace DataBaseManager
{
    class connect
    {
        public MySqlConnection conn = null;//连接对象
        public MySqlCommand comm = null;//语句执行对象
        public MySqlDataReader dr = null;//语句执行结果数据对象

        //用于一个窗口让用户自定义输入用户名与密码,在这里我直接定义好了
        //public string uid;
        //public string pwd;

        public void load()
        {
            //直接写链接语句,比较容易出错
            //conn = new MySqlConnection(
            //    "Database = data;Server = localhost;Port = 3306;Password = " + pwd + ";UserID = " + uid + ";charset = utf8mb4");
            //conn.Open();

            //使用Builder写语句,实现分段
            //与数据库连接的信息
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            //数据库连接时的用户名,可以用pid
            builder.UserID = "root";
            //数据库连接时的密码,可以用pwd
            builder.Password = "admin";
            //数据库连接时的服务器地址
            builder.Server = "localhost";
            //要连接的数据库
            builder.Database = "data";
            //定义与数据连接的链接
            conn = new MySqlConnection(builder.ConnectionString);
            //打开这个链接
            conn.Open();

        }
    }
}

         Then double-click the button in the form to define the input method to verify the connection.

 private void button1_Click(object sender, EventArgs e)
        {
            //con.uid = textBox1.Text;
            //con.pwd = textBox2.Text;

            //调用load方法建立open连接
            con.load();
            //查询语句,查询用户名与密码,这里的ls可以是textBox1.Text
            string sql = "select * from user_info where username ='ls';";
            con.comm = new MySqlCommand(sql, con.conn);
            con.dr = con.comm.ExecuteReader();
            con.dr.Read();

            //验证密码,同理,这里的123可以是textBox2.Text,因为我数据库设置的密码与账号是ls 与 123 所以无论输入什么都是登陆成功
            if ("123" == con.dr.GetString("password"))
            {
                MessageBox.Show("登录成功!");
                con.dr.Close();
                this.Close();//关闭此窗口,方便弹出主窗口
            }
            else
                MessageBox.Show("登录失败!");
        }

        If you enter the wrong information in connect, the following error will generally appear, and you need to check whether the account password is correct

         And if the database is entered incorrectly, this error will be displayed

Finally, the related database, the structure of the user_info table, can be created after referencing the dll file to directly test the C# form file

 In addition, a simple student information system with CRUD function is also put in the compressed package, which can be used for experience after in-depth study

 

 

The following is the table structure of student information, the relevant information can be entered by yourself

Link: https://pan.baidu.com/s/1Yg1aSgiydHQ1bevX5r3QCQ 
Extraction code: xyz3

Welcome to share and exchange!

Guess you like

Origin blog.csdn.net/laodaye_xiaolizi/article/details/131425011