当前位置:网站首页>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~
边栏推荐
- Fragmented NFT (Fractional NFT)
- understand js operators
- Android software security and reverse analysis reading notes
- 小米手机短信定位服务激活失败
- this points to the problem
- Build vulhub vulnerability shooting range on kali
- 【云原生】开源数据分析 SPL 轻松应对 T+0
- Digital twins will be an important way to enter the "metaverse"
- Flow control statement in js
- 安装Multisim出现 No software will be installed or removed解决方法
猜你喜欢

【云原生】开源数据分析 SPL 轻松应对 T+0

Install mysqldb in mac10.14

GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?
![[swagger close] The production environment closes the swagger method](/img/43/17be22626ba152b33beaf03f92fbec.png)
[swagger close] The production environment closes the swagger method

What is GameFi?

Why does read in bash need to cooperate with while to read the contents of /dev/stdin

【windows】--- SQL Server 2008 超详细安装教程

MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案

腾讯云GPU桌面服务器驱动安装

Hyper-V新建虚拟机注意事项
随机推荐
MySQL高级学习笔记
MySQL压缩包方式安装,傻瓜式教学
浅谈对分布式模式下CAP的理解
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
360 加固 file path not exists.
js中的this指向与原型对象
通信原理——纠错编码 | 汉明码(海明码)手算详解
quick-3.5 无法正常显示有混合纹理的csb文件
What is the difference between NFT and digital collection?
[uiautomation] Get WeChat friend list (stored in txt)
cocos2d-x-3.2 Physics
MySQL compressed package installation, fool teaching
cocos2d-x-3.x 修改和纪录
C语言 | 获取字符串里逗号间隔的内容
Regular Expression Basics
小米手机短信定位服务激活失败
2021年京东数据分析工程师秋招笔试编程题
What is GameFi?
CMOS管原理,及其在推挽电路中的应用
Why is the redis single-threaded also so fast?