请教s如何得到elect返回结果问题!!!
由于毕业设计需要在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;
}
问题点数:100、回复次数:7Top
1 楼wxqw()回复于 2004-05-01 07:19:02 得分 0
-------------------------------------------------------------
二。作者给出运行上述程序前做的准备
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.
Top
2 楼wxqw()回复于 2004-05-01 07:19:35 得分 0
望高手给以解答,我可以另开帖给分!Top
3 楼internetcsdn(2003-8-7 9:20:26)回复于 2004-05-01 08:28:50 得分 0
SELECT
从数据库中检索行,并允许从一个或多个表中选择一个或多个行或列。虽然 SELECT 语句的完整语法较复杂,但是其主要的子句可归纳如下:
SELECT select_list
[ INTO new_table ]
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
可以在查询之间使用 UNION 运算符,以将查询的结果组合成单个结果集。
Top
4 楼internetcsdn(2003-8-7 9:20:26)回复于 2004-05-01 08:30:13 得分 20
使用选择列表
选择列表可定义 SELECT 语句的结果集中的列。选择列表是以逗号分隔的一系列表达式。每个表达式定义结果集中的一列。结果集中列的排列顺序与选择列表中表达式的排列顺序相同。
结果集列的这些特性由选择列表中的下列表达式定义:
结果集列的数据类型、大小、精度以及小数位数与定义列的表达式相同。
结果集列的名称与定义列的表达式名称相关联。可选的 AS 关键字可用于更改名称,或当表达式没有名称时为其指定一个名称。
结果集列的数据值出自结果集的每一行的表达式的取值。
选择列表还可包含控制结果集的最终格式的关键字:
DISTINCT
DISTINCT 关键字可从结果集中除去重复的行。例如,在 Northwind Orders 表中有许多行的 ShipCity 值是相同的。若要获得已删除重复内容的 ShipCity 值列表,请用如下语句:
SELECT DISTINCT ShipCity, ShipRegion
FROM Orders
ORDER BY ShipCity
TOP n
TOP 关键字指定返回结果集的前 n 行。如果指定了 ORDER BY,行将在结果集排序之后选定。除非指定了 PERCENT 关键字,否则 n 即为返回的行数。PERCENT 指定 n 为结果集中返回的行的百分比。例如,SELECT 语句从 Orders 表中按照字母顺序返回前 10 个城市:
SELECT DISTINCT TOP 10 ShipCity, ShipRegion
FROM Orders
ORDER BY ShipCity
选择列表中的项目可包括:
简单表达式:对函数、局部变量、常量或者表或视图中的列的引用。
标量子查询是对结果集的每一行求得单值的 SELECT 语句。
通过对一个或多个简单表达式使用运算符创建的复杂表达式。
使用 * 关键字将返回表中的所有列。
以 @local_variable = expression 形式的变量赋值。SET @local_variable 语句还可用于变量赋值。
IDENTITYCOL 关键字将被解析为对具有 IDENTITY 属性的表中的列的引用。例如,已经为 Northwind Orders 表中的 OrderID 列定义了 IDENTITY 属性,这样,表达式 Orders.IDENTITYCOL 等同于 Orders.OrderID。
ROWGUILDCOL 关键字被解析为对表中具有 ROWGUIDCOL 属性的列的引用。
按照规定的语法,创建一个使用 IDENTITY 属性的新列(使用 SELECT INTO)。例如,若要在 authors 表中新建一列,其名称为 counter,数据类型为 int,并从数值 100 开始使后面的数字依次递增 1,应该使用 counter = IDENTITY(int, 100, 1)。
临时为查询结果添加一列以指定 CUBE 或 ROLLUP 操作是否添加行。使用 GROUPING 关键字。
如下示例显示了选择列表中可能包含的许多项目:
SELECT FirstName + ' ' + LastName AS "Employee Name",
IDENTITYCOL AS "Employee ID",
HomePhone,
Region,
10 AS Constant
FROM Northwind.dbo.Employees
ORDER BY LastName, FirstName ASC
Top
5 楼zjcxc(邹建)回复于 2004-05-02 09:17:27 得分 80
转到c++版去问,这个主要是程序代码的问题.
要select数据,对于数据库来说,只要发一条select语句给它就行了.
select * from 表
其他就是你程序中如何去发语句,和如何接收结果的问题.Top
6 楼wxqw()回复于 2004-05-02 13:29:40 得分 0
to zjcxc(邹建) :
对啊,你说的很对,不过我发了几个留言,没有人知道,郁闷!!!Top
7 楼zjcxc(邹建)回复于 2004-05-02 17:14:38 得分 0
应该很简单,至少VB如此,可惜我不会C++Top




