永发信息网

谁能写个C++(VC无关)连接数据库的程序

答案:2  悬赏:70  手机版
解决时间 2021-04-26 20:40
谁能写个C++(VC无关)连接SQLserver数据库的程序来参考一下,非常感谢!
最佳答案

// 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);
}



全部回答
http://www.vckbase.com/document/viewdoc/?id=1304 这里面有很详细
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
求电脑为什么有时候自动关机了?
我梦见自己又新找了个女朋友,什么意思呢?
中国工商银行朝阳春风支行停车场地址有知道的
最近,由河南天道置业公司赞助,组织一次生态
sadf是什么意思?
k4374列车经常晚点吗
有什么办法能在三个月之内省下或存到两千块钱
旺泰香庄在什么地方啊,我要过去处理事情
问道什么是王装70J
UUSee网络电视2010 怎样下载点播节目
今晚直播观看2010世锦赛中国C组比赛值班 中国
《东东不死传说》中生化男回血怎么按?要几格
2010年8月10号温州会有台风吗
问下这幅图是哪部动漫里的?
为什么AI版的电脑升级那么快?
推荐资讯
不小心把手机调没声音了,该怎么调回来?
肾石通有没有疗程
为什么我的左胸腔那里偶尔会绞痛?
想做点微商,像流量卡之类的
很急!咸宁职业技术学院的生物制药技术专业怎
人类的心灵品德怎样
我怎么没有死神塔的任务呢,塔就过到23层
如图,在四棱锥P-ABCD中,四边形ABCD是矩形,平
O型血男人跟O型血女人结婚生的宝宝是什么血型
有什么软件可以自动定时备份我的文档啊?
If can use to trade you to pause for a lif
最近飞轮海在上海有活动吗?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?