How to Setup MySQL Database in Visual Studio 2022 For a C++ Application?
Last Updated :
02 Nov, 2023
Databases play a crucial role in storing, retrieving, and managing data efficiently. MySQL, a popular open-source Relational Database Management System, is widely used for this purpose. C++ acts as the intermediary connecting user interfaces and MySQL databases, enabling developers to easily retrieve, edit, and store data. It provides the essential resources and libraries for establishing connections, running queries, and managing data smoothly.
This article provides a comprehensive guide on setting up a MySQL database with C++ in Visual Studio 2022 to enable database functionality for an application.
Configuring MySQL For C++ Application in Visual Studio
Essentially, the partnership between C++ and MySQL is a valuable collaboration that empowers developers to effortlessly build efficient, data-focused applications. This feature allows developers to tap into the complete potential of their data and deliver strong solutions to their users.
Follow the further steps to connect MySQL database with C++.
Step 1: Install Visual Studio 2022 Community Edition.
- Select and Install C++ Components for your desired project development.

Step 2: Install MySQL Connector/C++ Library.
- Download the MySQL Connector/C++ library from the MySQL website.
- Download Connector/C++ library zip for your system and placed it in a secure directory.

Step 3: Open Visual Studio 2022 and Select Create a new Project.
- Select a Empty C++ Project.

- Provide a name for your project and click Create.
- Right Click on Project Solution -> Add -> New Item . Add a new CPP file to project.

- Select appropriate Solution configuration (Release) and Platform (x64) as per downloaded Connector file.

Checkout this sample code for test purpose.
Example:
C++
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/statement.h>
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
using namespace std;
int main()
{
try {
sql::mysql::MySQL_Driver* driver;
sql::Connection* con;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://localhost:3306",
"root", "");
con->setSchema("test"); // your database name
sql::Statement* stmt;
stmt = con->createStatement();
// SQL query to create a table
string createTableSQL
= "CREATE TABLE IF NOT EXISTS GFGCourses ("
"id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,"
"courses VARCHAR(255) NOT NULL"
")";
stmt->execute(createTableSQL);
string insertDataSQL
= "INSERT INTO GFGCourses (courses) VALUES "
"('DSA'),('C++'),('JAVA'),('PYTHON')";
stmt->execute(insertDataSQL);
// SQL query to retrieve data from the table
string selectDataSQL = "SELECT * FROM GFGCourses";
sql::ResultSet* res
= stmt->executeQuery(selectDataSQL);
// Loop through the result set and display data
int count = 0;
while (res->next()) {
cout << " Course " << ++count << ": "
<< res->getString("courses") << endl;
}
delete res;
delete stmt;
delete con;
}
catch (sql::SQLException& e) {
std::cerr << "SQL Error: " << e.what() << std::endl;
}
return 0;
}
- Now RUN the project. You will see many error will appear including missing header files. Let' s move to the next step.
Step 4: Linking Connector/C++ Libraries with Project.
In this procedure we are setting up the environment for the application to locate necessary dependencies and linking libraries and drivers for the project which are necessary part of compilation process.
- Click on Project located at top-right corner , then select Project Properties (last option) .

- A pop up for Project configuration menu appears.

In the "Configuration Properties," configure the following settings:
- Verify the Configuration (Release) and Platform (x64) exactly as per project.

- Under "VC++ Directories" add the path to the MySQL Connector/C++ header files in "Include Directories."
( sql header files path ).
Header File Directories : your-downloaded-folder-path...\mysql-connector-c++-8.1.0-winx64\include\jdbc
- Under "VC++ Directories" add the path to the MySQL Connector/C++ library files in "Library Directories." ( .lib files path ).
Library Directories : your-downloaded-folder-path...\mysql-connector-c++-8.1.0-winx64\lib64\vs14
In this process we are linking necessary header files and libraries which helps to establish connection and link all MySQL drivers for the project.

- Under "C/C++" add the path to the MySQL Connector/C++ header files in to "Additional Include Directories."
Additional Include Directories : your-downloaded-folder-path...\mysql-connector-c++-8.1.0-winx64\include\jdbc
- In "C/C++" set the "Preprocessor Definitions" to include _CRT_SECURE_NO_WARNINGS; (just before NDEBUG; with semicolon ) to suppress warnings related to the use of certain C standard library functions.

Above processes ensures the compiler to relocate necessary MySQL files required for compilation and suppress unnecessary warnings.
- Under "Linker" choose " Input " add library " mysqlcppconn.lib" ( just file name ) to "Additional Dependencies" .

Adding Mysql Connection library to prepare database and application to ready for execution.
- Click "Apply" and then "OK"
Step 5: Now Rebuilt and Run the Project.
Step 6: There is a chance of getting an error of missing " .DLL " files .

What are .DLL files ?
DLL files contain code and data that multiple programs can use simultaneously. These files allow programs to share common code and resources. DLLs contain functions, classes, and resources that can be shared across multiple applications. This means that developers can create a single DLL file that performs specific tasks (e.g., handling graphics or database operations) and then have multiple programs use that DLL to perform those tasks without duplicating the code.
Follow the below instructions.

- Again click on Project located at top-right corner , then select Project Properties (last option) .
- In your project's properties, go to "Configuration Properties" > "Build Events" > "Post-Build Event."
- Under Command Line add a command to copy all DLLs to the output directory.
Command : xcopy /y "your-folder-path\mysql-connector-c++-8.1.0-winx64\lib64\*.dll"
This command will copy all DLLs from the specified directory to the output directory, ensuring they are available when you run your application from Visual Studio.
- Click on " Apply " and " OK ".
Step 5: Rebuilt and Run the project.

- Updated Records in Database.

Similar Reads
How to Install and Setup Visual Studio for C#?
Installing and setting up Visual Studio is the first step for developers to build, compile, and run C# applications. Whether you are a beginner or an experienced programmer, Visual Studio provides a powerful integrated development environment (IDE) for writing and debugging C# code. With Visual Stud
3 min read
How to Install and Setup Visual Studio for ASP.NET?
Visual Studio is an Integrated Development Environment(IDE) developed by Microsoft to develop GUI(Graphical User Interface), Web applications, console, web apps, mobile apps, cloud, and web services, etc. To install and use Visual Studio for the commercial purpose one must buy a license from Microso
4 min read
How to Install a C# Class Library in Visual Studio?
A C# library project is a separate project used to hold utility classes. So this might be the class that handles our database or might handle some communications with the network. In our case, we are going to create a math library that is a stand-in for some of those other cases. It is a single sour
3 min read
How to Install and Setup Live Server Extensions on Visual Studio code?
Visual Studio Code is one of the most sought-after IDE (Integrated Development Interface) of the current time. It helps the developers to efficiently write, build and test software over a single platform by combining and providing a multilateral set of functions. Visual Studio Code supports various
3 min read
How to Install and Use Packages in Visual Studio for MacOS?
In this article, we will learn how to install and use a package in Visual Studio for MacOS. Visual Studio is an Integrated Development Environment(IDE) developed by Microsoft to develop GUI(Graphical User Interface), console, Web applications, web apps, mobile apps, cloud, web services, etc. Native
2 min read
How to Import and Export Data to Database in MySQL Workbench?
Utilizing MySQL Workbench, a unified visual database designing or graphical user interface tool is necessary when working with database architects, developers, and administrators. It was made and is updated by Oracle. It offers comprehensive administration tools for server configuration, user admini
3 min read
How to Install Visual Studio Code on Windows?
Visual Studio Code (VS Code) is a free, lightweight, and open-source code editor developed by Microsoft. It supports multiple operating systems, including Windows, macOS, and Linux. VS Code is designed to be versatile and user-friendly, offering features such as:Syntax Highlighting and Autocompletio
3 min read
How to Stop MySQL Server on Windows and Linux?
MySQL is one of the most popular open-source relational database management systems. Whether we are managing a small development setup or a production environment knowing how to start and stop MySQL servers is essential. In this guide, we will learn about the steps to start and stop MySQL servers on
3 min read
How to Connect SQLite3 Database using Node.js ?
Connecting SQLite3 database with Node.js involves a few straightforward steps to set up and interact with the database. SQLite is a self-contained, serverless, zero-configuration, transactional SQL database engine, making it ideal for small to medium-sized applications. Hereâs how you can connect an
2 min read
How to Connect to Mysql Server Using VS Code and Fix errors?
MySQL is a relational database management system based on SQL-Structured Query Language used for accessing and managing records in a database. It can be easily connected with programming languages such as Python, Java, and PHP to serve various purposes that require CRUD ( Create,Read,Update,Delete)
4 min read