当前位置:网站首页>(vs2019 MFC connects to MySQL) make a simple login interface (detailed)

(vs2019 MFC connects to MySQL) make a simple login interface (detailed)

2022-06-26 08:08:00 Live in the heart of sunshine

Preface

1、 System environment :Window10,VS2019,mysql-8.0.23-winx64 ( Notice that it's all 64 bit)

2、 This is an easy one MFC Login interface of . Program , Bloggers put it in the following link , You can download it yourself . This blog post is a tutorial .

SupermarketCashierS.zip-C++ Document resources -CSDN download

3、 The main reference links of this article are as follows , But the sad thing is that I found myself using VS2019 It's not in the version ODBC Wizard, So I checked some information before I solved this problem .

If you use a version other than VS2019, You can try the following method to connect to the database . 

MFC+MySQL(ODBC data source ) Realize the detailed tutorial of database login ( use vs Realization

4、 Digression : Bloggers plan to build a simple supermarket cashier system , This is only the first step .

 

One 、 establish MFC Program

1、 open Visual Studio 2019, Click on “ Create a new project ”, choice “ MFC application ”.

 

by the way , If your VS No installation MFC,

You can enter the file path “ C:\Program Files (x86)\Microsoft Visual Studio\Installer ” 

double-click “ setup.exe ” , open Visual Studio Installer, Click on “ modify ”,

Installation .( Please install the contents in the red box )

 

2、 choice “ MFC application ” after , Click on “ next step ”, Fill in “ Project name ” and “ Location ” Then click “ establish ”, Application type selection “ Based on dialog ”, Click on “ complete ”. The project name of the blogger here is “ SupermarketCashierS ”.

Two 、 Basic contents of login interface

1、 Make the following simple login interface with the control , Right click to select “ Add the class ” , Fill in the class name as “ CLogin ”; Right click again to select  “ Add variables ”, Set the category of the control to “ value  ”, Fill in “ name ” and  “ visit ”. Attribute the password box to “ Behavior ” Of “ password ” Set to true.

 

 

3、 ... and 、 Connect mysql database

This is the third point , Be careful .

1、 First, the blogger's mysql The version is 64 bit Of , Attention should be paid to the . So all the platforms used are x64, Click again “ Configuration manager ” It's about , Set the platforms to  x64.

In particular , Configure the properties of the “ Environmental Science ” Fill in the installation for your system mysql Under the path of bin Folder , namely “ PATH = D:\mysql\mysql-8.0.23-winx64\bin;  ”, Don't forget the semicolon .

 

 

2、 Before you install mysql In the folder of , find “ lib ” Under folder “ libmysql.lib ” and “ libmysql.dll ”, Copy these two files to create MFC Under the same level directory of the program , In addition, it should also be put , Pictured .

 

3、 modify “ VC++ Catalog ” Medium Contains the directory 、 Reference directory 、 The library catalog , The paths are installed in your system mysql The path of include、lib、lib Folder .

Revise “ C/C++ ” The conventional , and   “ The linker ” General and input , See the picture .

 

Four 、 Set up mysql database

1、 First remember to start mysql service , Run with administrator first DOS Interface , Input “net stop mysql”, stop it mysql service , Press again “ Win + R ”, Input “ cmd ”, As shown in the figure below :

PS C:\WINDOWS\system32> net start mysql
MySQL  The service is starting  ..
MySQL  Service started successfully .
 
PS C:\WINDOWS\system32>

 

2、 Create database admins, form admin , The input fields are as follows :

 

5、 ... and 、 Code up

1、 stay “ pch.h ” Add a line of code to “ #include<afxdb.h> ”.

 

2、 double-click “ Sign in ” Button , Jump to “ void CSupermarketCashierSDlg::OnBnClickedOk() ” in . Fill in the code below :

void CSupermarketCashierSDlg::OnBnClickedOk()
{
	// Login Button
	//CDialogEx::OnOK();

	GetDlgItem(IDC_EDIT1)->GetWindowText(m_name); // Get the entered user name 
	GetDlgItem(IDC_EDIT2)->GetWindowText(m_psw); // Get the password you entered 

	const char user[] = "root"; // Fill in your  mysql  user name 
	const char pswd[] = "123456";  // Fill in your  mysql  password 
	const char host[] = "localhost";
	const char database[] = "admins";  // Fill in your  mysql  Database name 
	unsigned int port = 3306;

	MYSQL_RES* res;
	MYSQL_ROW row;
	MYSQL mysqlCon;

	if (m_name.IsEmpty() || m_psw.IsEmpty())
	{
		MessageBox(_T(" User name or password cannot be empty !"), _T(" User login information "));
		return;
	}

	mysql_init(&mysqlCon);
	if (!mysql_real_connect(&mysqlCon, host, user, pswd, database, port, NULL, 0))
	{
		AfxMessageBox(_T(" Failed to access database !"));
	}
	else
	{
		mysql_query(&mysqlCon, "SET USER GBK"); // Set character set 
		AfxMessageBox(_T(" Access to database succeeded !"));
	}


	//mysql_query(&myCont, "SET NAMES GBK"); // Set the encoding format , Otherwise, in the cmd Can't display Chinese  
    //  If your user name is Chinese , Add the above sentence ;

	int ress = mysql_query(&mysqlCon, "select name, psw from admin");

	if ( ress == 0 ) // The successful detection query is 0, Failure is not 0
	{
		res = mysql_store_result(&mysqlCon); // Save the queried data to  res
		if (mysql_num_rows(res) == 0) // The query result is empty 
		{
			AfxMessageBox(_T(" The user doesn't exist "));
		}
		else
		{

			row = mysql_fetch_row(res);
			if (m_psw == row[1])
			{
				mysql_free_result(res);
				MessageBox(_T(" Login successful !"));
			}
			else
			{
				AfxMessageBox(_T(" Wrong password !"));
			}
		}


	}
	else
	{
		AfxMessageBox(_T(" The visit to fail !"));
	}

	mysql_close(&mysqlCon);

}

 

PS:

(1) If you forget your mysql password , You can read this blog post of the blogger :

Win10 mysql-8.0.23-winx64 forget mysql Password solution ( The detailed steps )

https://blog.csdn.net/qq_34438969/article/details/116647580

 

(2) If you are in the mysql Set the password of the login user name to a number , To modify the character set to use a multibyte character set .

 

6、 ... and 、 Run code , The following screen appears , The successful

 

 

 

原网站

版权声明
本文为[Live in the heart of sunshine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170604410638.html