MFC中使用ADO访问Oracle

18 篇文章 0 订阅
13 篇文章 0 订阅

2014年3月26日17:28:15

MFC中使用ADO访问Oracle

通过执行SQL语句访问数据库。

_ConnectionPtr pConnection=NULL;//连接  
	_RecordsetPtr  pRecordset=NULL;//记录集  

	::CoInitialize(NULL);   
	pConnection.CreateInstance(__uuidof(Connection));  
	pRecordset.CreateInstance(__uuidof(Recordset));   

	if(!pConnection->State)  
	{  
		_bstr_t strConnect =_T("Provider=MSDAORA;Data Source=ORCL;User ID=etc_ctrlr;Password=itsmoe");  

		CString sInfo;
		try  
		{  
			pConnection->Open(strConnect, _T(""),_T(""), adModeUnknown);  
			sInfo.Format(_T("连接成功"));
		}catch (_com_error e)  
		{
			sInfo.Format(_T("连接失败"));
		}  
		MessageBox(sInfo);
	} 

	_bstr_t bstrPointSQL(L"select * from LWW_TEST");
	try
	{
		pRecordset->Open(bstrPointSQL, pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
		_variant_t var;
		int nSn=0;int nX=0;int nY=0;
		CString str;
		while (!pRecordset->EndOfFile)
		{
			var = pRecordset->GetCollect(L"SN");
			nSn = var.intVal;

			var = pRecordset->GetCollect(L"X");
			nX = var.intVal;

			var = pRecordset->GetCollect(L"Y");
			nY = var.intVal;

			str.Format(_T("SN:%d;X:%d;Y:%d"),nSn,nX,nY);
			//MessageBox(str);

			pRecordset->MoveNext();
		}
		pRecordset->Close();

	}catch (_com_error  e)
	{
		exit(-1);
	}

	try
	{
		CString sInsert;
		sInsert.Format(_T("insert into LWW_TEST (SN,x,y) values(4,4,4)"));
		pConnection->Execute((_bstr_t)sInsert,NULL,adCmdText);
	}catch (_com_error &e)
	{
		CString sInfo;
		sInfo.Format(_T("添加数据失败:%s"),e.ErrorMessage());
		MessageBox(sInfo);
		exit(-1);
	}

	try
	{
		CString sInsert;
		sInsert.Format(_T("update LWW_TEST set x=5 where SN=4"));
		pConnection->Execute((_bstr_t)sInsert,NULL,adCmdText);
	}catch (_com_error &e)
	{
		CString sInfo;
		sInfo.Format(_T("更新数据失败:%s"),e.ErrorMessage());
		MessageBox(sInfo);
		exit(-1);
	}

	if (pRecordset->State)  
		pRecordset->Close();  
	if(pConnection->State)  
		pConnection->Close();  

	::CoUninitialize(); 


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值