当前位置:网站首页>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~
边栏推荐
猜你喜欢
Artifact SSMwar exploded Error deploying artifact.See server log for details
On the side of Ali, tell me what are the application scenarios of message middleware you know?
What is an EVM Compatible Chain?
Common JVM interview questions and answers
Understanding of js arrays
【swagger关闭】生产环境关闭swagger方法
MySQL高级语句(一)
场效应管 | N-mos内部结构详解
腾讯云GPU桌面服务器驱动安装
【云原生】微服务Nacos的简单介绍与使用
随机推荐
Access database query
360 加固 file path not exists.
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
Common JVM interview questions and answers
Xiaobai learns reptiles - introduction to reptiles
数据库 | SQL增删改查基础语法
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
cocos2d-x-3.2图片灰化效果
SSH自动重连脚本
MySQL-如何分库分表?一看就懂
【windows】--- SQL Server 2008 超详细安装教程
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
cocos create EditBox 输入文字被刘海屏遮挡修改
cocoscreator3.5.2打包微信小游戏发布到QQ小游戏修改
unicloud 云开发记录
2021美赛C题M奖思路
unicloud 发布后小程序提示连接本地调试服务失败,请检查客户端是否和主机在同一局域网下
浅谈对分布式模式下CAP的理解
Year-end summary - the years are quiet~
jenkins +miniprogram-ci 一键上传微信小程序