| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 716 人关注过本帖
标题:[求助]SQL的调用问题?
只看楼主 加入收藏
lhj2005
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2007-1-23
收藏
 问题点数:0 回复次数:4 
[求助]SQL的调用问题?

比如一个表
id name weight height
01 a 80 180
02 b 77 177
03 c 76 172
. . . .
. . . .
. . . .
10 j 55 178

如何用C去调用数据,可以实现吗?

搜索更多相关主题的帖子: SQL name weight 数据 
2007-04-06 14:57
xiyou419
Rank: 1
等 级:新手上路
威 望:2
帖 子:104
专家分:0
注 册:2007-3-18
收藏
得分:0 
没试过,应该很复杂的,为什么不用C++,VB或.NET之类的???
我在网上看过2个例子,不过没看懂,希望对你有帮助吧.
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>

// Forward declarations of the error handler and message handler.
int err_handler(PDBPROCESS, INT, INT, INT, LPCSTR, LPCSTR);
int msg_handler(PDBPROCESS, DBINT, INT, INT, LPCSTR, LPCSTR,
LPCSTR, DBUSMALLINT);

main()
{
PDBPROCESS dbproc; // The connection with SQL Server.
PLOGINREC login; // The login information.
DBCHAR name[100];
DBCHAR city[100];

// Install user-supplied error- and message-handling functions.
dberrhandle (err_handler);
dbmsghandle (msg_handler);

// Initialize DB-Library.
dbinit ();

// Get a LOGINREC.
login = dblogin ();
DBSETLSECURE (login);
DBSETLAPP (login, "example");

// Get a DBPROCESS structure for communication with SQL Server.
dbproc = dbopen (login, "my_server");

// Retrieve some columns from the authors table in the
// pubs database.

// First, put the command into the command buffer.
dbcmd (dbproc, "SELECT au_lname, city FROM pubs..authors");
dbcmd (dbproc, " WHERE state = 'CA' ");

// Send the command to SQL Server and start execution.
dbsqlexec (dbproc);

// Process the results.
if (dbresults (dbproc) == SUCCEED)
{
// Bind column to program variables.
dbbind (dbproc, 1, NTBSTRINGBIND, 0, name);
dbbind (dbproc, 2, NTBSTRINGBIND, 0, city);

// Retrieve and print the result rows.
while (dbnextrow (dbproc) != NO_MORE_ROWS)
{
printf ("%s from %s\n", name, city);
}
}

// Close the connection to SQL Server.
dbexit ();

return (0);
}

int err_handler (PDBPROCESS dbproc, INT severity,
INT dberr, INT oserr, LPCSTR dberrstr, LPCSTR oserrstr)
{
printf ("DB-Library Error %i: %s\n", dberr, dberrstr);
if (oserr != DBNOERR)
{
printf ("Operating System Error %i: %s\n", oserr, oserrstr);
}
return (INT_CANCEL);
}

int msg_handler (PDBPROCESS dbproc, DBINT msgno, INT msgstate,
INT severity, LPCSTR msgtext, LPCSTR server,
LPCSTR procedure, DBUSMALLINT line)
{
printf ("SQL Server Message %ld: %s\n", msgno, msgtext);
return (0);
}

道德往往可以弥补智慧的缺陷;但智慧永远不能填补道德的空白.
2007-04-06 15:36
xiyou419
Rank: 1
等 级:新手上路
威 望:2
帖 子:104
专家分:0
注 册:2007-3-18
收藏
得分:0 
/***********************************************************************
Copyright (c) 2000, Microsoft Corporation
All Rights Reserved.
***********************************************************************/

// SQLTESTC - SQL Data Server sample program for console-based Windows NT.
// This sample uses mixed mode security, other than Windows NT Authentication,
// to establish connections. To use Windows NT Authentication, please use
// DBSETLSECURE to set the secure connection flag.
// Make the necessary changes to the hard-coded values, such as server
// name, user name and password.


#define DBNTWIN32 // must identify operating system environment
#include "windows.h"

#include <sqlfront.h>
#include <sqldb.h> // DB-LIB header file (should always be included)
#include <stdio.h>


main ()
{
PDBPROCESS dbproc; // allocate a DB-LIB process structure
PLOGINREC login; // allocate a DB-LIB login structure

// Variables used to store the returning data
char au_lname[41];
char au_fname[20];
char id[12];
char phone[13];
char address[41];
char city[21];
char state[3];
char zip[6];
char getname[41];
char Servername[25];
RETCODE result_code;

// Forward declarations of the error handler and message handler.
int err_handler(PDBPROCESS, int, int, int, char*, char*);
int msg_handler(PDBPROCESS, DBINT, int, int, char*);

if (dbinit() == (char *)NULL)
{
printf("Communications layer not loaded\n");
return(1);
}

// Install the user-supplied error-handling and message-handling
// routines. They are defined at the bottom of this source file.

dberrhandle((DBERRHANDLE_PROC)err_handler);
dbmsghandle((DBMSGHANDLE_PROC)msg_handler);

// Get server's computer name
Servername[0] = '\0';
printf ("\nEnter Name of SQL Server: ");
gets (Servername);

login = dblogin(); // get login record from DB-LIB
DBSETLUSER (login, (char *)"sa"); // set the username
DBSETLAPP (login, (char *)"sqltestc"); // set the application name
DBSETLPWD (login, (char *)""); // set the SQL Server password
DBSETLVERSION(login,DBVER60);
// To use secure, or trusted, connection, uncomment the following line.
// DBSETLSECURE (login);

// Now attempt to create and initialize a DBPROCESS structure
if ((dbproc = dbopen (login, Servername)) == NULL)
{
printf ("dbopen failed\n");
return (1); // exit program
}

dbuse (dbproc, "pubs"); // use the "pubs" database

while (TRUE)
{
printf ("\nEnter author's last name to retrieve (return to exit): ");
gets (getname);

if (getname[0] == '\0') // if only a return was entered
break;

// construct command buffer to be sent to the SQL server
dbcmd (dbproc, (char *)"select au_id, au_lname, au_fname, phone,");
dbcmd (dbproc, (char *)" address, city, state, zip");
dbcmd (dbproc, (char *)" from authors");
dbcmd (dbproc, (char *)" where au_lname = '");
dbcmd (dbproc, (char *)getname);
dbcmd (dbproc, (char *)"'");

dbsqlexec (dbproc); // send command buffer to SQL server

// now check the results from the SQL server
while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
{
if (result_code == SUCCEED)
{
dbbind (dbproc, 1, NTBSTRINGBIND, (DBINT) 0, (char *)id);
dbbind (dbproc, 2, NTBSTRINGBIND, (DBINT) 0, (char *)au_lname);
dbbind (dbproc, 3, NTBSTRINGBIND, (DBINT) 0, (char *)au_fname);
dbbind (dbproc, 4, NTBSTRINGBIND, (DBINT) 0, (char *)phone);
dbbind (dbproc, 5, NTBSTRINGBIND, (DBINT) 0, (char *)address);
dbbind (dbproc, 6, NTBSTRINGBIND, (DBINT) 0, (char *)city);
dbbind (dbproc, 7, NTBSTRINGBIND, (DBINT) 0, (char *)state);
dbbind (dbproc, 8, NTBSTRINGBIND, (DBINT) 0, (char *)zip);

// now process the rows
while (dbnextrow(dbproc) != NO_MORE_ROWS)
{
printf ("Author ID: %s\n", id);
printf ("Last Name: %s\n", au_lname);
printf ("First Name: %s\n", au_fname);
printf ("Address: %s\n", address);
printf ("City: %s\n", city);
printf ("State: %s\n", state);
printf ("Zip Code: %s\n", zip);
printf ("Telephone: %s\n", phone);
printf ("\n");
}
}
else
{
printf ("Results Failed\n");
break;
}
}
} // while (TRUE)

// Close the connection and exit
dbexit();

return (0);
}

int err_handler(PDBPROCESS dbproc, int severity, int dberr, int oserr, char * dberrstr, char * oserrstr)
{
if (dberrstr != NULL)
printf("DB-LIBRARY error:\n\t%s\n", dberrstr);

if (oserr != DBNOERR)
printf("Operating-system error:\n\t%s\n", oserrstr);

if ((dbproc == NULL) || (DBDEAD(dbproc)))
return(INT_EXIT);
else
return(INT_CANCEL);
}

int msg_handler(PDBPROCESS dbproc, DBINT msgno, int msgstate, int severity, char * msgtext)
{
printf("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
msgno, msgstate, severity, msgtext);
return(0);
}

道德往往可以弥补智慧的缺陷;但智慧永远不能填补道德的空白.
2007-04-06 15:38
lhj2005
Rank: 1
等 级:新手上路
帖 子:230
专家分:0
注 册:2007-1-23
收藏
得分:0 
谢谢啊
二个都是用C来做的吗

[此贴子已经被作者于2007-4-7 0:30:49编辑过]

2007-04-06 15:52
帅哥一条虫
Rank: 1
等 级:新手上路
威 望:1
帖 子:65
专家分:0
注 册:2006-10-15
收藏
得分:0 
看不懂
2007-04-07 19:30
快速回复:[求助]SQL的调用问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027642 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved