一个在TC中连接SQL的例子。
yjdn 2004-09-25 09:22:42 下面是一个在SQL中找到的一个(我理解是TC连接SQL的例子),但我执行不成功,我编译的时候,下面那两个头文件打不开,但在同一目录下的别的头文件math.h等都可以引用。
#include <sqlfront.h>
#include <sqldb.h>
这两个头文件可以在SQL的安装盘中找到。
下面是联机文档找到的例子。看看有没有谁可以用它来连接SQL,或者提供一个TC连SQL的成功例子。
Call-level Method
Microsoft® SQL Server™ 2000 programs, written using the DB-Library or ODBC API methods, communicate directly with SQL Server through C function calls. DB-Library or ODBC functions pass SQL statements to SQL Server and return the results of queries. The call-level method of programming requires no precompiler; however, you cannot include Transact-SQL statements in your C programs, as you can with Embedded SQL for C (ESQL/C).
For example, to use DB-Library to connect to SQL Server and execute a simple query against the pubs sample database requires source code similar to the following:
#define DBNTWIN32
#include <sqlfront.h>
#include <sqldb.h>
main()
{
DBPROCESS *dbproc;
LOGINREC *login;
RETCODE r;
dbinit();
login = dblogin();
if (login == NULL)
return (1);
DBSETLUSER(login, "my_login");
DBSETLPWD(login, "my_password");
dbproc = dbopen(login, "my_server");
dbfreelogin(login);
if (dbproc == NULL)
return (1);
dbuse(dbproc, "pubs");
dbcmd(dbproc,
"select au_fname from authors where au_lname = 'White'");
r = dbsqlexec(dbproc);
if (r == FAIL)
return (1);
while (1)
{
r = dbresults(dbproc);
if (r == SUCCEED)
{
/* Process the rows with dbnextrow() */
}
if ((r == FAIL) || (r == NO_MORE_RESULTS))
break;
}
return (0);
}
The DB-Library approach, using C function calls, is more verbose and more flexible than the ESQL/C approach, and it can be loosely coupled to any database structure. Because a great deal of the program's behavior can be changed dynamically, DB-Library programs are often general-purpose applications. DB-Library is well-suited for environments where the database structure is not known in advance.