c++中如何得到select返回结果问题!!!
由于毕业设计需要在www.codeproject.com找到一篇文章,主要关于如何利用c++连接并且操作sql server数据库的,文章写的很好,例子也是正确的,但是例子中演示了如何连接数据库,并且往sql server里插入(insert)一条记录的过程,由此可以懂得如何进行修改,删除的操作,但是查询(select)如何做到呢?
下面一是作者举的例子,二是作者的说明,请高手指点一下如何进行select操作,主要是如何得到返回结果!
分不够,可以再加!
--------------------------------------------------------
一:作者给的一个例子
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL;// Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "Test";// Data Source Name buffer
UCHAR szUID[10] = "test";// User ID buffer
UCHAR szPasswd[10] = "test";// Password buffer
UCHAR szModel[128];// Model buffer
SDWORD cbModel;// Model buffer bytes recieved
char buff[9] = "Testing";
UCHAR szSqlStr[128]= "INSERT into Quali (Colname) Values ('Testing')" ;
RETCODE retcode;
//sprintf((char*)szSqlStr,"INSERT into <Tablename> (Colname) Values ('%s')",buff);
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "test" using userid and password.
retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
// Prepare the SQL statement by assigning it to the statement handle
retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
// Project only column 1 which is the models
SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);
// Get row of data from the result set defined above in the statement
retcode = SQLFetch (hStmt);
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect (hDBC);
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
return 0;
下面一是作者举的例子,二是作者的说明,请高手指点一下如何进行select操作,主要是如何得到返回结果!
分不够,可以再加!
--------------------------------------------------------
一:作者给的一个例子
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
HSTMT hStmt = NULL;// Statement handle
UCHAR szDSN[SQL_MAX_DSN_LENGTH] = "Test";// Data Source Name buffer
UCHAR szUID[10] = "test";// User ID buffer
UCHAR szPasswd[10] = "test";// Password buffer
UCHAR szModel[128];// Model buffer
SDWORD cbModel;// Model buffer bytes recieved
char buff[9] = "Testing";
UCHAR szSqlStr[128]= "INSERT into Quali (Colname) Values ('Testing')" ;
RETCODE retcode;
//sprintf((char*)szSqlStr,"INSERT into <Tablename> (Colname) Values ('%s')",buff);
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "test" using userid and password.
retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
// Prepare the SQL statement by assigning it to the statement handle
retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
// Project only column 1 which is the models
SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);
// Get row of data from the result set defined above in the statement
retcode = SQLFetch (hStmt);
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect (hDBC);
}
// Free the allocated connection handle
SQLFreeConnect (hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv (hEnv);
return 0;
...全文
相关推荐
如果找不到匹配项,返回的结果是什么
当`WHERE`子句没有找到任何匹配项时,`SELECT COUNT(*)`返回的结果是什么?让我们深入探讨这个问题。在SQL中,`COUNT(*)`函数用于计算一个表中的行数,包括所有列的所有行。
c++实现win socket 编程中的Select I/O模型
本文将深入探讨如何使用C++在Windows环境下实现Socket编程中的Select I/O模型。
select模型_c++网络通信模型_
**处理结果**:select返回后,通过FD_ISSET宏检查哪些描述符准备好进行读写操作,然后对每个就绪的描述符进行相应的网络操作,如接收数据、发送数据等。
Windows Sockets 5单元 select 源码 + 注释 C++
返回结果:当某个条件满足时,select函数返回,并修改传入的fd_set结构体,标记出准备好的套接字。
EchoTCPServer-select.cpp
EchoTCPServer-select.cpp


