c++中如何得到select返回结果问题!!!
wxqw 2004-05-05 11:17:21 由于毕业设计需要在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;