VC使用ADO连接远程oracle数据库
_ConnectionPtr pConn;//连接对像
_RecordsetPtr pRect;//记录集对象
_CommandPtr pCmd;//命令对象
pRect.CreateInstance(__uuidof(Recordset));
pConn.CreateInstance(__uuidof(Connection));//L连接对像
pCmd.CreateInstance(__uuidof(Command));//命令对象
_bstr_t strConn = "Provider=OraOLEDB.Oracle.1;User ID=%s;Password=%s;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=%s))(CONNECT_DATA=(SERVICE_NAME = %s)));Persist Security Info=False"; //这个是远程
//strConn = "Provider=OraOLEDB.Oracle.1;Persist Security Info=True;DATABASE=%s;User ID=%s;Password= %s;"; //这个是本地
HRESULT rValue = pConn->Open(strConn,"","",adModeUnknown);
if (rValue == S_FALSE) { return; }
CString str = "select * from cc_bank";
pRect = pConn->Execute(_bstr_t(str),NULL,adCmdText);
pRect->MoveFirst();
while (pRect->adoEOF==VARIANT_FALSE)
{
CString str = LPSTR(_bstr_t(pRect->GetCollect("cnname")));
MessageBox(str);
pRect->MoveNext();
}
pRect->Close();
pConn->Close();
pRect.Release();
pConn.Release();
pCmd.Release();
}
catch(_com_error &e)
{
MessageBox(e.ErrorMessage());
}