C++如何连接SQL Server 2000
- 提问者网友:嘚啵嘚啵
- 2021-05-04 22:33
- 五星知识达人网友:猎心人
- 2021-05-04 22:49
// database.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define DBNTWIN32
#include <stdio.h>
#include <windows.h>
#include "sqlfront.h"
#include "sqldb.h"
#pragma comment(lib,"NTWDBLIB.LIB")
int err_handler(PDBPROCESS,INT,INT,INT,LPCSTR,LPCSTR);
int msg_handler(PDBPROCESS,DBINT,INT,INT,LPCSTR,LPCSTR,
LPCSTR,DBUSMALLINT);
int main()
{
PDBPROCESS sqlcmd; // 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();
DBSETLUSER(login,"user"); //登录用户名
DBSETLPWD(login,"sqluser"); //登录密码
//DBSETLAPP(login,"test_xhz"); //登录数据库名
// Get a DBPROCESS structure for communication with SQL Server.
sqlcmd=dbopen(login,".");
// Retrieve some columns from the "authors" table in the
// "pubs" database.
// First, put the command into the command buffer.
dbcmd(sqlcmd,"select * from angel..cpb"); //执行数据库命令
// Send the command to SQL Server and start execution.
dbsqlexec(sqlcmd);
// Process the results.
if (dbresults(sqlcmd)==SUCCEED)
{
// Bind column to program variables.
dbbind(sqlcmd,1,NTBSTRINGBIND,0,(LPBYTE)name);
dbbind(sqlcmd,2,NTBSTRINGBIND,0,(unsigned char *)city);
// Retrieve and print the result rows.
while (dbnextrow(sqlcmd)!=NO_MORE_ROWS)
{
printf("%s from %s\n",name,city);
}
}
// Close the connection to SQL Server.
dbexit();
return (0);
}
int err_handler(PDBPROCESS sqlcmd,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 sqlcmd,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);
}
我以前做的 ~
编译环境VS2005