MFC ADO 使用stream访问Oracle大字段

13 篇文章 0 订阅

2014年3月27日14:42:32

MFC ADO 使用stream访问Oracle大字段(注意Provider)

代码:

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

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

	_bstr_t strConnect =_T("Provider=OraOLEDB.Oracle.1;Data Source=ORCL;User ID=etc_ctrlr;Password=itsmoe");

	if(!pConnection->State)  
	{    

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

	_bstr_t bstrSQL(_T("select * from LWW_BLOB"));
	try
	{
		pRecordset->Open(bstrSQL, pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);

		_variant_t vtSource;
		vtSource.vt = VT_ERROR;
		vtSource.scode = DISP_E_PARAMNOTFOUND;
		_StreamPtr pStream;
		pStream.CreateInstance(__uuidof(Stream));
		pStream->Type = adTypeBinary;
		pStream->Open(vtSource,adModeUnknown,adOpenStreamUnspecified,_bstr_t(_T("")),_bstr_t(_T("")));
		
		if(!pRecordset->EndOfFile) 
			pRecordset->MoveLast();

		/*pRecordset->AddNew();
		pRecordset->PutCollect(_T("SN"),(_variant_t)(123));

		pStream->LoadFromFile(_bstr_t(_T("1.txt")));
		_variant_t vtBlob = pStream->Read(adReadAll);
		pRecordset->Fields->GetItem(_T("ParaData"))->PutValue(vtBlob);*/

		pRecordset->AddNew();
		pRecordset->PutCollect(_T("SN"),(_variant_t)(456));

		pStream->LoadFromFile(_bstr_t(_T("blob.jpg")));
		_variant_t vtBlob = pStream->Read(adReadAll);
		pRecordset->Fields->GetItem(_T("ParaData"))->PutValue(vtBlob);

		pRecordset->Update();
		if(pStream->State == adStateOpen)
			pStream->Close();
		else
			pStream->Release();

		pRecordset->Close(); 
	}catch (_com_error &e)
	{
		CString sInfo;
		sInfo.Format(_T("写大字段失败:%s"),e.ErrorMessage());
		MessageBox(sInfo);
		exit(-1);
	}

	_bstr_t bstrSQL1(_T("select * from LWW_BLOB where SN=1"));
	try
	{
		pRecordset->Open(bstrSQL1, pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);

		_variant_t vtSource;
		vtSource.vt = VT_ERROR;
		vtSource.scode = DISP_E_PARAMNOTFOUND;
		_StreamPtr pStream;
		pStream.CreateInstance(__uuidof(Stream));
		pStream->Type = adTypeBinary;
		pStream->Open(vtSource,adModeUnknown,adOpenStreamUnspecified,_bstr_t(_T("")),_bstr_t(_T("")));

		pStream->LoadFromFile(_bstr_t(_T("2.txt")));
		_variant_t vtBlob = pStream->Read(adReadAll);
		pRecordset->Fields->GetItem(_T("ParaData"))->PutValue(vtBlob);

		if(pStream->State == adStateOpen)
			pStream->Close();
		else
			pStream->Release();

		pRecordset->Close(); 
	}catch (_com_error &e)
	{
		CString sInfo;
		sInfo.Format(_T("更新大字段失败:%s"),e.ErrorMessage());
		MessageBox(sInfo);
		exit(-1);
	}

	_bstr_t bstrSQL2(_T("select * from LWW_BLOB where SN=456"));
	try
	{
		pRecordset->Open(bstrSQL2, pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);

		_variant_t vtSource;
		vtSource.vt = VT_ERROR;
		vtSource.scode = DISP_E_PARAMNOTFOUND;
		_StreamPtr pStream;
		pStream.CreateInstance(__uuidof(Stream));
		pStream->Type = adTypeBinary;
		pStream->Open(vtSource,adModeUnknown,adOpenStreamUnspecified,_bstr_t(_T("")),_bstr_t(_T("")));

		_variant_t vtBlob = pRecordset->Fields->GetItem(_T("ParaData"))->GetValue();

		pStream->Write(vtBlob);

		pStream->SaveToFile(_bstr_t(_T("a.jpg")),adSaveCreateOverWrite);

		if(pStream->State == adStateOpen)
			pStream->Close();
		else
			pStream->Release();

		pRecordset->Close(); 
	}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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值