当前位置:网站首页>VS connects to MYSQL through ODBC (2)
VS connects to MYSQL through ODBC (2)
2022-07-31 06:00:00 【Lobster is peeling my shell】
In the last blog we shared the configuration steps, this one will share how to read data through code
I. Result display


If you want data like this, the following code may help
Second, code sharing
#include#include#include #include#include using namespace std;int main() {SQLHENV serverhenv;SQLHDBC serverhdbc;SQLHSTMT serverhstmt;SQLRETURN ret;SQLCHAR cno[20] = { 0 }, cname[20] = { 0 }, semester[10] = { 10 };//SQLINTEGER grade = 0, length;SQLLEN grade = 0, length;// allocate environment handleret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &serverhenv);//set environment propertiesret = SQLSetEnvAttr(serverhenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);if (!SQL_SUCCEEDED(ret)) {cout << "AllocEnvHandle error!" << endl;system("pause");}// allocate connection handleret = SQLAllocHandle(SQL_HANDLE_DBC, serverhenv, &serverhdbc);if (!SQL_SUCCEEDED(ret)) {cout << "AllocDbcHandle error!" << endl;system("pause");}//Database linkage//ret = SQLConnect(serverhdbc, (SQLCHAR*)"odbc_test(data source name)", SQL_NTS, (SQLCHAR*)"root(database user name)", SQL_NTS, (SQLCHAR*)"123456(database user password)", SQL_NTS);ret = SQLConnect(serverhdbc, (SQLCHAR*)"vsodbc07", SQL_NTS, (SQLCHAR*)"root", SQL_NTS, (SQLCHAR*)"root", SQL_NTS);//Connect to the databaseif (!SQL_SUCCEEDED(ret)) {cout << "SQL_Connect error!" << endl;system("pause");}// allocate the execution statement handleret = SQLAllocHandle(SQL_HANDLE_STMT, serverhdbc, &serverhstmt);//Execute the SQL statement//ret = SQLExecDirect(serverhstmt, (SQLCHAR*)"insert into course values('C01','database','spring',NULL);", SQL_NTS);//insert data//ret = SQLExecDirect(serverhstmt, (SQLCHAR*)"update course set semester='Autumn' where cno='C01';", SQL_NTS);//Modify data//ret = SQLExecDirect(serverhstmt, (SQLCHAR*)"delete from course where cno='C01';", SQL_NTS);//delete dataret = SQLExecDirect(serverhstmt, (SQLCHAR*)"select * from course;", SQL_NTS);if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO){// bind dataSQLBindCol(serverhstmt, 1, SQL_C_CHAR, (void*)cno, sizeof(cno), &length);SQLBindCol(serverhstmt, 2, SQL_C_CHAR, (void*)cname, sizeof(cname), &length);SQLBindCol(serverhstmt, 3, SQL_C_CHAR, (void*)semester, sizeof(semester), &length);//Move the cursor to the next row, that is, get the downlink data//while (SQL_NO_DATA != SQLFetch(serverhstmt)) {for (int i = 0; i<3; i++) {if(SQL_NO_DATA != SQLFetch(serverhstmt)){cout << "cno:" << cno << " cname:" << cname << " semester:" << semester;cout << "55"<< endl;}}}// release the statement handleret = SQLFreeHandle(SQL_HANDLE_STMT, serverhstmt);if (SQL_SUCCESS != ret && SQL_SUCCESS_WITH_INFO != ret)cout << "free hstmt error!" << endl;// disconnect the database connectionret = SQLDisconnect(serverhdbc);if (SQL_SUCCESS != ret && SQL_SUCCESS_WITH_INFO != ret)cout << "disconnected error!" << endl;// release the connection handleret = SQLFreeHandle(SQL_HANDLE_DBC, serverhdbc);if (SQL_SUCCESS != ret && SQL_SUCCESS_WITH_INFO != ret)cout << "free hdbc error!" << endl;// release the environment handle handleret = SQLFreeHandle(SQL_HANDLE_ENV, serverhenv);if (SQL_SUCCESS != ret && SQL_SUCCESS_WITH_INFO != ret)cout << "free henv error!" << endl;system("pause");} Both blogs are about VS connecting to MYSQL through ODBC
The next article will share VS direct connection to MYSQL
Welcome to discuss~
边栏推荐
- 为什么bash中的read要配合while才能读取/dev/stdin的内容
- Yuan prospect and four track of the universe
- 计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
- MySQL压缩包方式安装,傻瓜式教学
- SQLite 查询表中每天插入的数量
- 360 hardening file path not exists.
- sqlmap injection tutorial common commands
- UiBot has an open Microsoft Edge browser and cannot perform the installation
- cocoscreator 显示刘海内容
- cocos create EditBox 输入文字被刘海屏遮挡修改
猜你喜欢

最新MySql安装教学,非常详细

著名网站msdn.itellyou.cn原理分析

Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?

unicloud 云开发记录

mysql password modification method in Linux (pro-test available)

js中的this指向与原型对象
![[JVM Loading]---Class Loading Mechanism](/img/b6/d1754cb6699d18602ca9a463571c0c.png)
[JVM Loading]---Class Loading Mechanism

The server time zone value ‘й‘ is unrecognized or represents more than one time zone

The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered
![[Cloud native] Open source data analysis SPL easily copes with T+0](/img/89/4a96358956782ef9dacf0b700b54c3.png)
[Cloud native] Open source data analysis SPL easily copes with T+0
随机推荐
SSH自动重连脚本
MySql创建数据表
Android软件安全与逆向分析阅读笔记
对js的数组的理解
quick lua加密
Several solutions for mysql startup error The server quit without updating PID file
Build vulhub vulnerability shooting range on kali
浅谈对分布式模式下CAP的理解
Why is the redis single-threaded also so fast?
[uiautomation] Get WeChat friend list (stored in txt)
[swagger close] The production environment closes the swagger method
阿里一面,说说你知道消息中间件的应用场景有哪些?
C language tutorial (3) - if and loop
"limit" query in Oracle database
flutter arr 依赖
Eternal blue bug reappears
Sqlite column A data is copied to column B
jenkins +miniprogram-ci 一键上传微信小程序
js中的this指向与原型对象
微信小程序源码获取与反编译方式