VS2019 connects to mysql8.0 database tutorial with pictures and text

VS2019 connects to mysql8.0 database tutorial with pictures and text

1. First, prepare VS2019 and MySQL database. Both can be downloaded from the official website. We will directly describe the connection process.

2. Connection:

Step 1: Open the MySQL installation directory. My local installation directory is as follows: (Note whether there are include and lib folders)

insert image description here

Step 2: Open VS2019 and create a new empty project.

Step 3: Right-click the project name to open the property page:

insert image description here

Step 4: Open VC++ directory, in the include directory, add the path of the include file in the mysql installation file here:

insert image description here

Step 5: Still on the property page, open C/C++, select General, and add the include file path in the mysql file in the additional include directory as in the previous step:

insert image description here

Step 6: Still on the property page, click the linker option, click General, and add the lib file path in the mysql installation folder to the additional library directory:

insert image description here

Step 7: Still in the linker of the property page, click the second input and change Add the libmysql.lib file. Note that Add the dependency name libmysql.lib without adding the path. This file is also in the lib directory of the MySQL installation folder:

insert image description here
insert image description here

Step 8: Check the platform at the top of the property page and select x64. x32 may cause an error.

insert image description here

Step 9: Copy bin\libmysql.dll in the MySQL installation directory to c:\windows\system32:

insert image description here
insert image description here

If you don't do this step, you will probably get an error when you run the program. libmysql.dll not found error.

3. Write test code:

First open the MySQL database. I use a local database table to test:

insert image description here

Test code:

#include <stdio.h>
#include "mysql.h"
#include<iostream>
using namespace std;
void main()
{
	MYSQL mysql; //A database structure MYSQL_RES* res; //A result set structure MYSQL_ROW row; //char** two-dimensional array, storing records one by one //Initialize the database mysql_init(&mysql);
	//Set encoding mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");
	//Connect to database //If the connection fails, the information about the connection failure will be displayed so that we can make corresponding modifications.
	// mysql_real_connect parameters: 2. Local address 3. Your mysql username 4. Your mysql password 5. Database name 6. Port number if (mysql_real_connect(&mysql, "localhost", "root", "root", "luckysheep", 3306, NULL, 0) == NULL) {
		cout << (mysql_error(&mysql));
	}
	//Query data mysql_query(&mysql, "SELECT * from department");
	//Get the result set res = mysql_store_result(&mysql);
	//Display data //Assign a value to ROW, determine whether ROW is empty, and print the data if it is not empty.
	while (row = mysql_fetch_row(res))
	{
		printf("%s ", row[0]); //Print ID
		printf("%s ", row[1]); //Print ID
		cout << endl;
	}
	//Free the result set mysql_free_result(res);
	//Close the database mysql_close(&mysql);
	//Stop and wait for getchar();
}

Be sure to change your mysql username and password.
Output:

insert image description here

Record: Actually, I wanted to connect vs2010 to mysql today, because the project was carried out in vs2010, and I wanted to connect to the database for data testing, etc. But after a whole day of installation, solving one error after another, it still failed in the end, and #include <stdbool.h> showed an error. After some research, I found out that the C language does not support the Boolean type built-in. By referencing the stdbool.h header file, I can make it support the Boolean type. But VS2010 does not fully support C99...stdbool.h is C99...Time is tight, I will check it out later when I have time. . . . .

insert image description here

Summarize

This is the end of this article about connecting VS2019 to MySQL 8.0 database. For more relevant content about connecting VS2019 to MySQL 8.0 database, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of the process and common problems of connecting VS2019 to MySQL database

<<:  Example code for implementing dynamic skinning with vue+element

>>:  How to configure VMware multi-node environment

Recommend

Swiper.js plugin makes it super easy to implement carousel images

Swiper is a sliding special effects plug-in built...

VMware Workstation Pro 16 License Key with Usage Tutorial

VMware Workstation is a powerful desktop virtual ...

Vue implements anchor positioning function

This article example shares the specific code of ...

JavaScript to achieve magnifying glass effect

This article shares the specific code for JavaScr...

Chinese and English font name comparison table (including Founder and Arphic)

In CSS files, we often see some font names become...

Detailed explanation of JavaScript array deduplication

Table of contents 1. Array deduplication 2. Dedup...

Detailed explanation of jQuery chain calls

Table of contents Chain calls A small case Chain ...

Implementation of Docker container connection and communication

Port mapping is not the only way to connect Docke...

CSS3 flip card number sample code

I received a task from the company today, and the...

jQuery plugin to implement minesweeper game (2)

This article shares the second article of using j...

MYSQL custom function to determine whether it is a positive integer example code

You can write a function: Mainly use regular expr...

Let's talk about parameters in MySQL

Preface: In some previous articles, we often see ...

MySQL 8.0.18 deployment and installation tutorial under Windows 7

1. Preliminary preparation (windows7+mysql-8.0.18...

CSS container background 10 color gradient Demo (linear-gradient())

grammar background: linear-gradient(direction,col...