请问VC如何使用ADO

visual 2000-07-12 11:19:00
我查了一下msdn,好像没有CAdo之类的咚咚,到是有CDao.
如您方便,请给段源码,谢谢!
...全文
189 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
visual 2000-11-29
  • 打赏
  • 举报
回复
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
//用 recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
// 也出错
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
------------------------------------------
好像open并不能返回记录总数吧。我用的是m_pRecordset->GetRecordCount(),还有就是m_pRecordset->adoEOF也不是返回记录总数,只是返回一个BOOL型数据,判断是否到了表底。
所以先open,然后GetRecordCount(要用adOpenKeyset,否则始终返回-1)


halbert 2000-11-22
  • 打赏
  • 举报
回复
关于vc++ 的ado 编程
atdafx.h
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF", "adoEOF")

BOOL CGetemailApp::InitInstance()
{
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox("ole 出错");
return FALSE;
}
::CoInitialize( NULL );
}
int CGetemailApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
return CWinApp::ExitInstance();
}

bool getrecordset()
{
char SqlStr[255],account[50];
char Error[500];
char putfilestr[300];
long recordnum;
m_pConnection = NULL;
m_pRecordset = NULL;
_variant_t TheEmail,ThePhone_office,TheAccount,TheUser_id;
char strEmail[50],strPhone_offiece[50],strAccount[50],strUser_id[50];
if(!OpenFileForReadWrite())
return ;
try{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
recordnum=m_pConnection->Open("DSN=stat","test","test",-1);
sprintf(putfilestr,"%20s %30s %40s %30s\n","用户编码","用户帐号","用户Email","用户办公电话");
fwrite(putfilestr,strlen(putfilestr),1,fpout);
while(!feof(fpin))
{
fscanf(fpin,"%s",account);
//sprintf(SqlStr,"select a.user_id,a.account, b.email,b.phone_office from userbasicinfo a,usercontactinfo b where a.account='%s' and b.user_id=a.user_id",account);
strcpy(SqlStr,"select * from region");
recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
//用 recordnum=m_pRecordset->Open(SqlStr,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
// 也出错
recordnum=m_pRecordset->adoEOF;
//执行到此出错,为何。跳到catch(...)
if(!m_pRecordset->adoEOF)
{
TheUser_id=m_pRecordset->GetCollect("user_id");

TheAccount=m_pRecordset->GetCollect("account");
TheEmail=m_pRecordset->GetCollect("email");
ThePhone_office=m_pRecordset->GetCollect("phone_office");
sprintf(putfilestr,"%20s %30s %40s %30s\n",TheUser_id.bstrVal,TheAccount.bstrVal,TheEmail.bstrVal,ThePhone_office.bstrVal);
fwrite(putfilestr,strlen(putfilestr),1,fpout);
}//end if
m_pRecordset->Close();
}//end while
}//end try
catch(_com_error *e)
{

//CString Error = e->ErrorMessage();
//char Error[500];
strcpy(Error , (char*)e->Description());
AfxMessageBox(e->ErrorMessage());
}
catch(_com_error e)
{

//CString Error = e->ErrorMessage();
strcpy(Error , (char*)e.Description());
//AfxMessageBox(e->ErrorMessage());
AfxMessageBox(Error);
}
catch(...)
{
AfxMessageBox("ado 出错");
}

m_pRecordset->Close();;
m_pConnection->Close();
m_pConnection->Release();
CloseAllFile();
}
// 执行到recordnum=m_pRecordset->adoEOF; 出错 为何
// 请那位大侠指点

visual 2000-07-13
  • 打赏
  • 举报
回复
多谢了,各位!
wangshuai 2000-07-13
  • 打赏
  • 举报
回复
使用VC++中的import指令,导入ado 的动态连接库msado15.dll,编译后,系统会自动产生ado的对应的C++类,很容易,记住了!
步骤:
1. 修改stdafx.h文件.
#endif // _AFX_NO_AFXCMN_SUPPORT //此行系统产生,
#include <comdef.h> //使用com类,会使数据库操作非常容易,一定要熟悉com!
#import "C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll" rename ("EOF","ADOEOF")
using namespace ADODB;
2.连接数据源
//import指令的结果之一产生一些智能指针类,省去引用计数之苦.
HRESULT hr;
_ConnectionPtr pConnection;
_RecordsetPtr pRecordset;
hr=pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
hr=pConnection->Open(_bstr_t(L"Provider=Microsoft.Jet.OLEDB.3.51;Data Source=C:\\毕业设计\\Supermarket2000.mdb;"),
_bstr_t(L""),_bstr_t(L""),adModeUnknown);
if(FAILED(hr))
{
AfxMessageBox("系统发生错误.",MB_OK);
pConnection->Close();
}
else
{
_RecordsetPtr pRecordset;
_variant_t vRecsAffected(0L);
pRecordset=pConnection->Execute(strQuery,&vRecsAffected,adOptionUnspecified);
if(pRecordset->GetADOEOF())
{
pRecordset->Close();
pConnection->Close(); }
else
{
pRecordset->Close();
pConnection->Close();

}

}

}
3.进行数据库操作--增加,修改,删除,......
以本人的毕业设计中一个程序片断为例:
bool CSupermarketView::AddNewAccount()
{
//增加新纪录
_RecordsetPtr pRecordset;
HRESULT hr;
_bstr_t Query("select * from AccountBook where Date is NULL");
_variant_t vNull;
vNull.vt=VT_ERROR;
vNull.scode=DISP_E_PARAMNOTFOUND;
hr=pRecordset.CreateInstance(__uuidof(Recordset));
if(SUCCEEDED(hr))
{
pRecordset->PutRefActiveConnection(m_pConnection);
hr=pRecordset->Open(_variant_t(Query),vNull,adOpenForwardOnly,adLockOptimistic,adCmdText);
if(SUCCEEDED(hr))
{
COleSafeArray vaFieldList;
vaFieldList.CreateOneDim(VT_VARIANT,5);
long lArrayIndex[1];
lArrayIndex[0]=0;
vaFieldList.PutElement(lArrayIndex,&(_variant_t("Date")));
lArrayIndex[0]=1;
vaFieldList.PutElement(lArrayIndex,&(_variant_t("LastBalance")));
lArrayIndex[0]=2;
vaFieldList.PutElement(lArrayIndex,&(_variant_t("Balance")));
lArrayIndex[0]=3;
vaFieldList.PutElement(lArrayIndex,&(_variant_t("TurnInFund")));
lArrayIndex[0]=4;
vaFieldList.PutElement(lArrayIndex,&(_variant_t("TotalFund")));

COleSafeArray vaValueList;
vaValueList.CreateOneDim(VT_VARIANT,5);
lArrayIndex[0]=0;
vaValueList.PutElement(lArrayIndex,&(vDate));
lArrayIndex[0]=1;
vaValueList.PutElement(lArrayIndex,&(vLastBalance));
lArrayIndex[0]=2;
vaValueList.PutElement(lArrayIndex,&(vBalance));
lArrayIndex[0]=3;
vaValueList.PutElement(lArrayIndex,&(vTurnInFund));
lArrayIndex[0]=4;
vaValueList.PutElement(lArrayIndex,&(vTotalFund));

pRecordset->AddNew(&vaFieldList,&vaValueList);
pRecordset->Close();
return true;
}
}
return false;
}
其它操作,自己可要"摸索"了,多看import指令产生的文件.有了心得,可别旺了通知我一声,祝你好运!
WHQ 2000-07-13
  • 打赏
  • 举报
回复
_ConnectionPtr/_RecordsetPtr/_CommandPtr之类的类就是ADO的
zzh 2000-07-13
  • 打赏
  • 举报
回复
有一本书,叫用VC进行数据库编程,介绍了VC有关的数据库操作,包括ODBC,ADO,DAO,
OLE DB,可以参考一下。VC知识库中也不相关的介绍.
jun 2000-07-13
  • 打赏
  • 举报
回复
msdn中有一个adosamp程序,就是用ado的。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧