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;

...全文
116 4 点赞 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
justaseeker 2004-05-05
这个可以这样解决,根据返回记录的格式定义一个响应的记录,如MyTable来接受返回结果
  • 打赏
  • 举报
回复
林石公 2004-05-05
请看一下游标相关资料
  • 打赏
  • 举报
回复
wxqw 2004-05-05
-------------------------------------------------------------
二。作者给出运行上述程序前做的准备

Introduction
This article explains how to get connected to SQL Server database and then writes string in the database table using an SQL statement.

Setting up a database
Ask your DBA - Database administrator to do the following
1.Create a table named test.
2.Provide a Username and Password to connect to it.
3.Get the name of the Server.

Setting up the DSN - Data Source Name
You need to create a Data Source Name which identifies the Server and the table to which you have to connect. Do the following steps to set up a DSN
1.Goto Control Panel/Administrative Tools/Data Sources(ODBC).
2.In the User DSN tab click Add.In the list that appears select SQL Server and click Finish.
3.In the first step of the DNS Configuration wizard that pops up give any name u want to identify your DSN.
4.Select the Server on which the database exists,click Next.
5.Select the radio button for SQL Server authentication using a Login ID and Password.
6.In the Client configuration command button select TCP/IP.
7.Check the box for 'Connect to SQL Server to obtain default settings for the additional configuration options.
8.Provide the Username and Password your DBA has provided ,click Next.
9.Check the box 'Change the default database to' and enter the name of your table.
10.Accept the defaults and perform the test connection.
我在别的版块儿问了,没有得到有建设性的回答,谢谢各位了!
  • 打赏
  • 举报
回复
ghtsao 2004-05-05
晕,改一下就可以了嘛,

szSqlStr[128]= "SELECT * FROM Quali" ;

后面取:
retcode = SQLFetch (hStmt);
  • 打赏
  • 举报
回复
相关推荐
发帖
C++ 语言
加入

6.0w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
申请成为版主
帖子事件
创建了帖子
2004-05-05 11:17
社区公告
暂无公告