VC在控制台模式下访问Oracle数据库的操作指令,代码如下:
#include <afx.h>
#include <iostream>
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")
using namespace std;
#define BUFSIZE 64
int main()
{
//char buf[BUFSIZE];
//int index;
_ConnectionPtr m_pConnection;
CoInitialize(NULL);//初始化COM组件
HRESULT hr=m_pConnection.CreateInstance(__uuidof(Connection));
if(SUCCEEDED(hr))
{
cout《"创建Connection对象成功!"《endl;
}
try
{
_bstr_t strConnect="Provider=OraOLEDB.Oracle;User ID=scott;Password=19900624;Persist Security Info=True;Data Source=\"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.108.85.137)(PORT = 1521)) )(CONNECT_DATA = (SID = ORCL)(SERVER=DEDICATED)))\"";
hr=m_pConnection->Open(strConnect,"","",NULL);
if(SUCCEEDED(hr))
cout《"数据库已连接!"《endl;
}
catch(_com_error e) //捕捉异常
{
cout《e.ErrorMessage()《e.Description()《endl;
//cout《"error"《endl;
return FALSE;
}
/////////////////查询数据库操作//////////////////////
_variant_t var1;
_RecordsetPtr m_pRecordset=NULL; //创建一个数据集智能指针
m_pRecordset.CreateInstance(__uuidof(Recordset));//初始化Recordset指针
char *content;
char type;
cin》type;
switch(type)
{
case 's':
try
{
CString sql;
sql="select * from DB_USER";
m_pRecordset=m_pConnection->Execute((_bstr_t)sql,NULL,adCmdText);
}
catch(_com_error *e)
{
cout《e->ErrorMessage()《endl;
}
try
{
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
else
{
cout《"表内为空"《endl;
}
while(!m_pRecordset->adoEOF)
{
//var1=m_pRecordset->Fields->GetItem("USERNAME")->GetValue();
var1=m_pRecordset->GetCollect("USERNAME");
m_pRecordset->MoveNext();
if(var1.vt!=VT_NULL)
{
content=_com_util::ConvertBSTRToString((_bstr_t)var1);
}
cout《"从数据库得到数据:"《content《endl;
}
}
catch(_com_error *e)
{
cout《e->ErrorMessage()《endl;
}
break;
case 'd':