Build a C# WPF project to connect to the MySQL database hand in hand

1. Install MySQL

Download the MySQL installation package link: https://dev.mysql.com/downloads/ , as shown in the figure, clickMySQL Community Downloads

insert image description here
Continue to select MySQL Community Server, as shown in the figure

insert image description here
Select the version to be downloaded, select the first one for Download, as shown in the figure:

insert image description here
Click No thanks on this page, just start my download

insert image description here
After the download is complete, unzip it directly, write the MySQL configuration file, and create a new my.ini file, as shown in the figure:

insert image description here
The content of the file is as follows:

[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录   ----------Mysql解压之后的文件路径-------------
basedir=D:\mysql-8.0.34-winx64
# 设置mysql数据库的数据的存放目录  ---------文件夹自行创建
datadir=D:\mysql-8.0.34-winx64\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。
max_connect_errors=10
# 服务端使用的字符集默认为utf8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
#mysql_native_password
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8

Note that # 设置mysql数据库的数据的存放目录 ---------文件夹自行创建 datadir=D:\mysql-8.0.34-winx64\data
it is best to create a new data folder in the decompression directory, otherwise you may not be able to enter mysql later

insert image description here
Open CMD as an administrator and enter the bin directory of mysql, as shown in the figure:

insert image description here
Excuting an order

mysqld --initialize --console

As shown in the figure, the initial password is different for each person, and the position is marked with a yellow mark in the figure

insert image description here
Install mysql service

mysqld --install mysql

then start

net start mysql

as the picture shows:

insert image description here
input the command

mysql -uroot -p

Then enter the initial password:

insert image description here
Change login password

ALTER USER 'root'@'localhost' IDENTIFIED BY '666666';

change the password to666666

insert image description here
Exit mysql, enter

exit

To stop the mysql service, enter

net stop mysql

Configure environment variables, as shown in the figure:

insert image description here
New in system variables

变量名:MYSQL_HOME
变量值:MySQL的解压目录

as the picture shows:

insert image description here
Then find PATH in the system variable, add

%MYSQL_HOME%\bin

as the picture shows:

insert image description here

2. DBeaver installation (not required)

DBeaver is a database connection tool, free, cross-platform

Download URL: https://dbeaver.io/download/

insert image description here
After decompression or installation directly, as shown in the figure:

insert image description here
Open the software, as shown in the figure:

insert image description here
Select MySQL, as shown in the figure:

insert image description here
Enter the password, just set666666

insert image description here
Download the driver file automatically, as shown in the figure:

insert image description here
start mysql

insert image description here
The connection is successful, as shown in the figure:

insert image description here
Can operate on the database:

insert image description here

3. C# WPF connects to MySQL

Create a project, as shown in the figure:

insert image description here
Create a WPF application:

insert image description here
Create project:

insert image description here
Design interface:

insert image description here
Place controls:

insert image description here
Automatically install the required dll package, as shown in the figure:

insert image description here
After the installation is complete, the error message disappears:

insert image description here
Connect to MySQL, as shown in the figure:

insert image description here
Run the project:

insert image description here
Click the button and the connection is successful:

insert image description here
The test code is as follows:

using MySql.Data.MySqlClient;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace FM_NVM_App
{
    
    
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
    
    
        public MainWindow()
        {
    
    
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
    
    

            Process p = Process.Start("E:\\software\\FM_FIG-Trans_V1.0\\dist\\FM_FIG-Trans_GUI\\FM_FIG-Trans_GUI.exe");
            p.WaitForExit();
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
    
    

            String strConn = "Database = sys;Server = localhost;Port = 3306;Password = 666666;UserID = root;charset = utf8";
            MySqlConnection conn = new MySqlConnection(strConn);
            MySqlCommand comm = new MySqlCommand();
            comm.Connection = conn;
            try
            {
    
    
                conn.Open();
                txtbox.Text = strConn;
            }
            catch (Exception ex)
            {
    
    
                MessageBox.Show(ex.Message);
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
    
    

        }
    }
}

I hope this article is helpful to everyone. If there is anything wrong with the above, please correct me.

Sharing determines the height, and learning widens the gap

Guess you like

Origin blog.csdn.net/qq_42078934/article/details/131839111