| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1024 人关注过本帖
标题:c++用ado连接数据库时的问题
只看楼主 加入收藏
dbgcarlos
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2005-2-7
收藏
 问题点数:0 回复次数:2 
c++用ado连接数据库时的问题

#include "StdAfx.h"
#include "iostream.h"
#include<stdio.h>
#include <iomanip.h>
#include <stdlib.h>

class CangKu
{
public:
char productId[20];
char productName[30];
char categoryId[10];
char productor[20];
char introduce[200];
float price;

CangKu(){}
~CangKu(){}

void query()
{
::CoInitialize(NULL); //初始化OLE/COM库环境

CangKu ck;
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t bstrSQL("select * from product");

try
{
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect= "Provider=SQLOLEDB;Server=(local);Database=login; uid=sa; pwd=2004944020a;";
//若数据库在网络上则Server为形如(192.168.1.5,3340)
//用户sa和密码空只是针对我的库
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!\n";
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t vsid,vsproductname,vscategoryId,vsproductor,vsintroduce,vsprice; //对应库中的字段
cout << "编号 商品名 产品分类号 生产厂家 产品介绍 价格";
cout << "\n----------------------------------------------------------------\n";
while(!m_pRecordset->adoEOF)
{
vsid = m_pRecordset->GetCollect(_variant_t((long)0));//这儿给字段编号和字段名都可以
vsproductname = m_pRecordset->GetCollect("productName");
vscategoryId = m_pRecordset->GetCollect("pCategoryId");
vsproductor = m_pRecordset->GetCollect("productor");
vsintroduce = m_pRecordset->GetCollect("introduce");
vsprice = m_pRecordset->GetCollect("price");
if (vsid.vt != VT_NULL && vsproductname.vt != VT_NULL && vscategoryId.vt != VT_NULL&& vsproductor.vt != VT_NULL && vsintroduce.vt != VT_NULL && vsprice.vt != VT_NULL)
{
cout.setf(ios::left);
cout << setw(14) << (char*)(_bstr_t)vsid;//setw(14)表示左边空出14个空格
cout << setw(14) << (char*)(_bstr_t)vsproductname;
cout << setw(8) << (char*)(_bstr_t)vscategoryId;
cout << setw(8) <<(char*)(_bstr_t)vsproductor;
cout <<setw(20) << (char*)(_bstr_t)vsintroduce;
cout <<setw(20) << (char*)(_bstr_t)vsprice;
cout.unsetf(ios::left);
cout << endl;
}

m_pRecordset->MoveNext();///移到下一条记录
}

//m_pRecordset->Update();
//m_pConnection->Execute(query_cmd,NULL,1); //用Execute执行sql语句来删除
//m_pRecordset->Delete(adAffectCurrent);删除当前数据
m_pRecordset->Close(); // 关闭记录集
}
catch(_com_error e)
{
cerr << "\nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
}


void insert()
{
::CoInitialize(NULL); //初始化OLE/COM库环境

CangKu ck;
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t bstrSQL("select * from product");
//char * query_cmd = "DELETE FROM book WHERE bookname = '怕'";
try
{
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect= "Provider=SQLOLEDB;Server=(local);Database=login; uid=sa; pwd=2004944020a;";
//若数据库在网络上则Server为形如(192.168.1.5,3340)
//用户sa和密码空只是针对我的库
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!\n";
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t vsid,vsproductname,vscategoryId,vsproductor,vsintroduce,vsprice; //对应库中的字段
cout << "编号 商品名 产品分类号 生产厂家 产品介绍 价格";
cout << "\n----------------------------------------------------------------\n";
while(!m_pRecordset->adoEOF)
{
vsid = m_pRecordset->GetCollect(_variant_t((long)0));//这儿给字段编号和字段名都可以
vsproductname = m_pRecordset->GetCollect("productName");
vscategoryId = m_pRecordset->GetCollect("pCategoryId");
vsproductor = m_pRecordset->GetCollect("productor");
vsintroduce = m_pRecordset->GetCollect("introduce");
vsprice = m_pRecordset->GetCollect("price");
if (vsid.vt != VT_NULL && vsproductname.vt != VT_NULL && vscategoryId.vt != VT_NULL&& vsproductor.vt != VT_NULL && vsintroduce.vt != VT_NULL && vsprice.vt != VT_NULL)
{
cout.setf(ios::left);
cout << setw(14) << (char*)(_bstr_t)vsid;//setw(14)表示左边空出14个空格
cout << setw(14) << (char*)(_bstr_t)vsproductname;
cout << setw(8) << (char*)(_bstr_t)vscategoryId;
cout << setw(8) <<(char*)(_bstr_t)vsproductor;
cout <<setw(20) << (char*)(_bstr_t)vsintroduce;
cout <<setw(20) << (char*)(_bstr_t)vsprice;
cout.unsetf(ios::left);
cout << endl;
}

m_pRecordset->MoveNext();///移到下一条记录
}
cout << "\n----------------------------------------------------------------\n";
cout << "\n请输入你要添加的商品信息\n";
cout << "编号:";
cin >>ck.productId;
cout << "\n商品名:";
cin >>ck.productName;
cout << "\n产品分类号:";
cin >>ck.categoryId;
cout << "\n生产厂家:";
cin >>ck.productor;
cout<<"\n商品介绍:";
cin>>ck.introduce;
cout << "\n价格:";
cin >> ck.price;
m_pRecordset->MoveFirst(); //移动到第一条记录
m_pRecordset->AddNew(); ///添加新记录
m_pRecordset->PutCollect("productId",_variant_t(ck.productId));
m_pRecordset->PutCollect("productName",_variant_t(ck.productName));
m_pRecordset->PutCollect("pCategoryId",_variant_t(ck.categoryId));
m_pRecordset->PutCollect("productor",_variant_t(ck.productor));
m_pRecordset->PutCollect("introduce",_variant_t(ck.introduce));
m_pRecordset->PutCollect("price",_variant_t(ck.price));
m_pRecordset->Update();
//m_pConnection->Execute(query_cmd,NULL,1); //用Execute执行sql语句来删除
//m_pRecordset->Delete(adAffectCurrent);删除当前数据
m_pRecordset->Close(); // 关闭记录集
}
catch(_com_error e)
{
cerr << "\nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
}

void del()//未实现
{
::CoInitialize(NULL); //初始化OLE/COM库环境

CangKu ck;
char pid[20];
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t bstrSQL("select * from product");
//char * query_cmd = "DELETE FROM product WHERE productId ='"+pid+"'";
try
{
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect= "Provider=SQLOLEDB;Server=(local);Database=login; uid=sa; pwd=;";
//若数据库在网络上则Server为形如(192.168.1.5,3340)
//用户sa和密码空只是针对我的库
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!\n";
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t vsid,vsproductname,vscategoryId,vsproductor,vsintroduce,vsprice; //对应库中的字段
cout << "编号 商品名 产品分类号 生产厂家 产品介绍 价格";
cout << "\n----------------------------------------------------------------\n";
while(!m_pRecordset->adoEOF)
{
vsid = m_pRecordset->GetCollect(_variant_t((long)0));//这儿给字段编号和字段名都可以
vsproductname = m_pRecordset->GetCollect("productName");
vscategoryId = m_pRecordset->GetCollect("pCategoryId");
vsproductor = m_pRecordset->GetCollect("productor");
vsintroduce = m_pRecordset->GetCollect("introduce");
vsprice = m_pRecordset->GetCollect("price");
if (vsid.vt != VT_NULL && vsproductname.vt != VT_NULL && vscategoryId.vt != VT_NULL&& vsproductor.vt != VT_NULL && vsintroduce.vt != VT_NULL && vsprice.vt != VT_NULL)
{
cout.setf(ios::left);
cout << setw(14) << (char*)(_bstr_t)vsid;//setw(14)表示左边空出14个空格
cout << setw(14) << (char*)(_bstr_t)vsproductname;
cout << setw(8) << (char*)(_bstr_t)vscategoryId;
cout << setw(8) <<(char*)(_bstr_t)vsproductor;
cout <<setw(20) << (char*)(_bstr_t)vsintroduce;
cout <<setw(20) << (char*)(_bstr_t)vsprice;
cout.unsetf(ios::left);
cout << endl;
}

m_pRecordset->MoveNext();///移到下一条记录
}
cout << "\n----------------------------------------------------------------\n";
cout << "\n请输入你要删除的商品编号\n"<<endl;
cin>>pid;

m_pRecordset->Update();
//m_pConnection->Execute(query_cmd,NULL,1); //用Execute执行sql语句来删除
//m_pRecordset->Delete(adAffectCurrent);删除当前数据
m_pRecordset->Close(); // 关闭记录集
}
catch(_com_error e)
{
cerr << "\nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
}

void modify()//未实现
{
::CoInitialize(NULL); //初始化OLE/COM库环境

CangKu ck;
::CoInitialize(NULL);
_RecordsetPtr m_pRecordset("ADODB.Recordset");
_ConnectionPtr m_pConnection("ADODB.Connection");
_bstr_t bstrSQL("select * from product ");

try
{
m_pConnection.CreateInstance("ADODB.Connection");
// 设置连接字符串,必须是BSTR型或者_bstr_t类型
_bstr_t strConnect= "Provider=SQLOLEDB;Server=(local);Database=login; uid=sa; pwd=2004944020a;";
//若数据库在网络上则Server为形如(192.168.1.5,3340)
//用户sa和密码空只是针对我的库
m_pConnection->Open(strConnect,"","",adModeUnknown);
if(m_pConnection==NULL)
cerr<<"Lind data ERROR!\n";
// 创建记录集对象
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 取得表中的记录
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
_variant_t vsid,vsproductname,vscategoryId,vsproductor,vsintroduce,vsprice; //对应库中的字段
cout << "编号 商品名 产品分类号 生产厂家 产品介绍 价格";
cout << "\n----------------------------------------------------------------\n";
while(!m_pRecordset->adoEOF)
{
vsid = m_pRecordset->GetCollect(_variant_t((long)0));//这儿给字段编号和字段名都可以
vsproductname = m_pRecordset->GetCollect("productName");
vscategoryId = m_pRecordset->GetCollect("pCategoryId");
vsproductor = m_pRecordset->GetCollect("productor");
vsintroduce = m_pRecordset->GetCollect("introduce");
vsprice = m_pRecordset->GetCollect("price");
if (vsid.vt != VT_NULL && vsproductname.vt != VT_NULL && vscategoryId.vt != VT_NULL&& vsproductor.vt != VT_NULL && vsintroduce.vt != VT_NULL && vsprice.vt != VT_NULL)
{
cout.setf(ios::left);
cout << setw(14) << (char*)(_bstr_t)vsid;//setw(14)表示左边空出14个空格
cout << setw(14) << (char*)(_bstr_t)vsproductname;
cout << setw(8) << (char*)(_bstr_t)vscategoryId;
cout << setw(8) <<(char*)(_bstr_t)vsproductor;
cout <<setw(20) << (char*)(_bstr_t)vsintroduce;
cout <<setw(20) << (char*)(_bstr_t)vsprice;
cout.unsetf(ios::left);
cout << endl;
}

m_pRecordset->MoveNext();///移到下一条记录
}
cout << "\n----------------------------------------------------------------\n";
cout << "\n请输入你要修改的商品信息编号\n";
cout << "编号:";
cin >>ck.productId;
cout << "\n商品名:";
cin >>ck.productName;
cout << "\n产品分类号:";
cin >>ck.categoryId;
cout << "\n生产厂家:";
cin >>ck.productor;
cout<<"\n商品介绍:";
cin>>ck.introduce;
cout << "\n价格:";
cin >> ck.price;
m_pRecordset->MoveFirst(); //移动到第一条记录
m_pRecordset->AddNew(); ///添加新记录
m_pRecordset->PutCollect("productId",_variant_t(ck.productId));
m_pRecordset->PutCollect("productName",_variant_t(ck.productName));
m_pRecordset->PutCollect("pCategoryId",_variant_t(ck.categoryId));
m_pRecordset->PutCollect("productor",_variant_t(ck.productor));
m_pRecordset->PutCollect("introduce",_variant_t(ck.introduce));
m_pRecordset->PutCollect("price",_variant_t(ck.price));
m_pRecordset->Update();
m_pRecordset->Close(); // 关闭记录集
}
catch(_com_error e)
{
cerr << "\nERROR:" << (char*)e.Description();//抛出异常
}
if(m_pConnection->State)
m_pConnection->Close();
::CoUninitialize();
}

};

int main()
{
CangKu ck;
int num;

while(1)
{
cout<<"欢迎光临商品管理系统"<<endl;
cout<<"1.查看现有商品"<<endl;
cout<<"2.增加现有产品"<<endl;
cout<<"3.修改现有商品的信息"<<endl;//未实现
cout<<"4.删除现有商品"<<endl;//未实现
cout<<"5.退出";
cout<<endl<<"请输入功能编号:"<<endl;

cin>>num;
switch(num)
{
case 1:ck.query();getchar();system("cls");continue;
case 2:ck.insert();system("cls");continue;
case 3:ck.modify();getchar();system("cls");continue;;
case 4:ck.del();getchar();system("cls");continue;
case 5:cout<<"欢迎下次使用,谢谢"<<endl;break;
default:break;
}
return 0;
}
}

删除和修改功能没有出来,我这个程序是根据别人的程序改的,对ado不是很了解,望大家能帮帮我。

搜索更多相关主题的帖子: ado 数据库 
2007-01-11 17:45
dbgcarlos
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2005-2-7
收藏
得分:0 
怎么改呢,希望大家帮我一下,我很急,谢谢

2007-01-12 12:35
adou
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:867
专家分:81
注 册:2006-8-24
收藏
得分:0 

把附件贴上来啊


大家好,才是真的好!
2007-01-17 16:47
快速回复:c++用ado连接数据库时的问题
数据加载中...
 
   



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

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