谁知道C++如何与sql2000如何连接 100

最好说明如何连接比如说用ODBC接口怎么连接,然后还有源代码(最好有注释啊)我是新手,呵呵。... 最好说明如何连接比如说用ODBC接口怎么连接,然后还有源代码(最好有注释啊)
我是新手,呵呵。
展开
 我来答
匿名用户
2009-03-19
展开全部
下面的程序中有数据库连接,操作的详细的代码,你参照一下吧:)

#include <ole2.h>
#include <iostream.h>

#import "c:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename ("EOF", "EndOfFile")

/**********************全局变量*********************/

_ConnectionPtr pConnection;
HRESULT hr;
_RecordsetPtr pRecordset;
_bstr_t sql; //SQL语句
_bstr_t sqlCount; //
_variant_t vCount; //计算记录数
_variant_t RecordsAffected;

void Count()
{
//执行SQL统计命令得到包含记录条数的记录集
pRecordset = pConnection->Execute(sqlCount,&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
//取得第一个字段的值放入vCount变量
vCount = pRecordset->GetCollect(vIndex);
// pRecordset->Close();
cout<<"共有"<<vCount.lVal<<"条记录."<<endl;
}

void Book()
{
_variant_t vBookName, vAuthor, vWord, vPricing, vCategory, vISBN, vPrintTime;
try
{
while( !pRecordset->EndOfFile )
{
//取得各列的值,从0开始计数
vBookName = pRecordset->GetCollect("BookName");
vAuthor = pRecordset->GetCollect("Author");
vWord = pRecordset->GetCollect("Word");
vPricing = pRecordset->GetCollect("Pricing");
vCategory = pRecordset->GetCollect("Category");
vISBN = pRecordset->GetCollect("ISBN");
vPrintTime = pRecordset->GetCollect("PrintTime");
///输出记录集中的记录
if( vBookName.vt != VT_NULL)
{
cout<<"书名: "<<(_bstr_t)vBookName<<"\t"
<<"作者: "<<(_bstr_t)vAuthor<<" "
<<"字数: "<<vWord.lVal<<" "
<<"价格: "<<vPricing.lVal<<" "
<<"类别: "<<(_bstr_t)vCategory<<" "
<<"ISBN: "<<(_bstr_t)vISBN<<" "
<<"出版时间:"<<(_bstr_t)vPrintTime<<endl;
}
pRecordset->MoveNext();///移到下一条记录

}
}
catch (_com_error* e)
{
cout<<e->ErrorMessage()<<endl;
}
}

void Printing()
{
_variant_t vPrintNo, vBookName, vMaterialNo, vVolume, vPrintPrice;
try
{
while( !pRecordset->EndOfFile )
{
//取得各列的值,从0开始计数
vPrintNo = pRecordset->GetCollect("PrintNo");
vBookName = pRecordset->GetCollect("BookName");
vMaterialNo = pRecordset->GetCollect("MaterialNo");
vVolume = pRecordset->GetCollect("Volume");
vPrintPrice = pRecordset->GetCollect("PrintPrice");
///输出记录集中的记录
if( vPrintNo.vt != VT_NULL)
{
cout<<"印刷工作号: "<<vPrintNo.lVal<<"\t"
<<"书名: "<<(_bstr_t)vBookName<<" "
<<"材料编号: "<<vMaterialNo.lVal<<" "
<<"印刷数量: "<<vVolume.lVal<<" "
<<"印刷费用: "<<(_bstr_t)vPrintPrice<<endl;
}
pRecordset->MoveNext();///移到下一条记录

}
}
catch (_com_error* e)
{
cout<<e->ErrorMessage()<<endl;
}
}

void Material()
{
_variant_t vMaterialNo, vMaterialName, vUnitPrice;
try
{
while( !pRecordset->EndOfFile )
{
//取得各列的值,从0开始计数
vMaterialNo = pRecordset->GetCollect("MaterialNo");
vMaterialName = pRecordset->GetCollect("MaterialName");
vUnitPrice = pRecordset->GetCollect("UnitPrice");
///输出记录集中的记录
if( vMaterialNo.vt != VT_NULL)
{
cout<<"材料编号: "<<vMaterialNo.lVal<<"\t"
<<"材料名称: "<<(_bstr_t)vMaterialName<<" "
<<"材料价格: "<<(_bstr_t)vUnitPrice<<endl;
}
pRecordset->MoveNext();///移到下一条记录

}
}
catch (_com_error* e)
{
cout<<e->ErrorMessage()<<endl;
}
}

void RunG();

void SelectTable()
{
int nSelect;
while (true)
{
cout<<"数据库中有三个表:"<<endl
<<"\t1-->Book(书)"<<endl
<<"\t2-->Printing(印刷)"<<endl
<<"\t3-->Material(材料)"<<endl
<<"\t0-->返回上一菜单"<<endl
<<"请选择: ";
cin>>nSelect;

if (nSelect == 0)
{
cout<<endl;
RunG();
}

if ( (nSelect > 3) || (nSelect < -1) )
{
cout<<endl<<"选择错误!请重新输入!"<<endl;
continue;
}

switch (nSelect)
{
case 1:
sql = "SELECT * FROM Book";
sqlCount = "SELECT COUNT(*) FROM Book";
cout<<"Book";
break;
case 2:
sql = "SELECT * FROM Printing";
sqlCount = "SELECT COUNT(*) FROM Printing"; //印刷任务
cout<<"Printing";
break;
case 3:
sql = "SELECT * FROM Material";
sqlCount = "SELECT COUNT(*) FROM Material";
cout<<"Material";
break;
}

Count(); //////////

if (vCount.lVal == 0) //如果记录为0,则不遍历
{
return ;
}

pRecordset.CreateInstance(__uuidof(Recordset));
pRecordset->Open(sql,_variant_t((IDispatch*)pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if (nSelect == 1)
{
Book();
}
if (nSelect == 2)
{
Printing();//印刷任务
}
if (nSelect == 3)
{
Material();
}
}
}

void RunG()
{
int nSelect;
bool inputFlag = false;
cout<<"********对数据库简单操作********"<<endl
<<"1->查询: ";
cin>>nSelect;

while ( !inputFlag )
{
switch (nSelect)
{
case 1:
SelectTable();
inputFlag = true;
break;
default:
inputFlag = false;
cin.clear();
cout<<"选择错误!请重新输入: ";
cin>>nSelect;
}
}
}

void Data()
{

//******连接数据库********
try
{
_bstr_t sCon;
sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb";
hr = pConnection.CreateInstance(__uuidof(Connection));
if ( SUCCEEDED(hr) )
{
hr = pConnection->Open(sCon,"","",adModeUnknown);
cout<<"连接数据库成功!"<<endl;
}
}
catch (_com_error* e)
{
cout<<e->ErrorMessage()<<endl;
}

//**********建立数据集********
/*
try
{

//执行SQL命令:CREATE TABLE创建表格Book,Book包含四个字段:
//整形BookNo,字符串Author,整形Page,货币型Pricing,整形Category

sql = "CREATE TABLE Book(BookNo INTEGER,Author TEXT,Page INTEGER,Pricing Currency,CategoryNo TEXT )";
pConnection->Execute(sql,&RecordsAffected,adCmdText);
//往表格里面添加记录
sql = "INSERT INTO Book(BookNo,Author,Page,Pricing,CategoryNo) VALUES (1, 'Washington',250,15.4,'小说')";
pConnection->Execute(sql,&RecordsAffected,adCmdText);
///将所有记录Pricing字段的值加一
sql = "UPDATE Book SET Pricing = Pricing+1";
pConnection->Execute(sql,&RecordsAffected,adCmdText);

}
catch (_com_error* e)
{
cout<<e->ErrorMessage()<<endl;
} */
}

void CloseData()
{
//*********关闭数据集********
pRecordset->Close();
pRecordset = NULL;

//********关闭连接********
if ( pConnection->State )
{
pConnection->Close();
}
pConnection = NULL;
}

int main(int argc, char* argv[])
{
::CoInitialize(NULL);
Data();
RunG();
CloseData();
::CoUninitialize();

cout<<endl;
system("pause");
return 0;
}
lcht1986
2009-03-20 · 超过21用户采纳过TA的回答
知道答主
回答量:109
采纳率:0%
帮助的人:65.5万
展开全部
#import \"c:\Program Files\Common Files\System\ADO\msado15.dll\"
//必须的dll文件
no_namespace rename ("EOF", "EndOfFile")
//重命名EOF,防止eof名冲突,就是自己起个个性点的
_ConnectionPtr;
//连接字符串,新建一个txt,将扩展名改为udl,设置好,再用txt打开,最下面一行,将"Provider=什么什么;"去掉之后剩下的就是连接字符串
_ConnectionPtr类中有个Execute方法可以执行数据库命令,比如select * from table
_RecordsetPtr; //结果集,用于接收数据集的
//详细的去网上找,或者图书馆
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
Luma
2009-03-19 · TA获得超过9828个赞
知道大有可为答主
回答量:1647
采纳率:50%
帮助的人:1124万
展开全部
爱莫能助~(⊙o⊙)
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式