Visual Studio连接mysql数据库
时间: 2023-11-15 14:04:35 浏览: 69
可以使用MySQL Connector/C++连接MySQL数据库。以下是一个使用Visual Studio的示例代码:
```
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main() {
try {
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("localhost", "username", "password");
con->setSchema("database_name");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT * FROM table_name");
while (res->next()) {
cout << res->getString("column_name") << endl;
}
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "SQL exception: " << e.what() << endl;
return -1;
}
return 0;
}
```
阅读全文