mfc oracle数据库编程,MFC 连接Oracle 数据库

_ConnectionPtr pConnection = NULL;

pConnection.CreateInstance(_uuidof(Connection));

pConnection->Mode = adModeUnknown;

pConnection->ConnectionTimeout = 5;

pConnection->CommandTimeout = 5;

HRESULT hr = pConnection->Open("Provider=OraOLEDB.Oracle;Persist Security Info=True;User ID=sys;DataSource=\"(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST =192.168.5.49)(PORT = 1521)) )(CONNECT_DATA = (SID =huaneng)))\"", "test", "huangjiapeng",NULL);

if (SUCCEEDED(hr))

{

AfxMessageBox("连接数据库成功!");

}

//查询数据库

//执行命令

_RecordsetPtr pRecordset = NULL;

pRecordset.CreateInstance(_uuidof(Recordset));

hr = pRecordset->Open(_T("select * from person"), pConnection.GetInterfacePtr(), adOpenDynamic,adLockOptimistic, adCmdText);

if(SUCCEEDED(hr))

{

AfxMessageBox("查询成功");

}

else if(FAILED(hr))

{

AfxMessageBox("查询失败");

}

try

{

// 写入各字段值

CString m_Name = "hello";

CString m_Age = "98";

pRecordset->AddNew();

pRecordset->PutCollect("NAME", _variant_t(m_Name));

pRecordset->PutCollect("AGE", atol(m_Age));

pRecordset->Update();

AfxMessageBox("插入成功!");

//pRecordset->MoveFirst();

}

catch (_com_error *e)

{

AfxMessageBox(e->ErrorMessage());

}

try

{

// 假设删除第二条记录

pRecordset->MoveFirst();

pRecordset->Move(1);       // 从0开始

pRecordset->Delete(adAffectCurrent); // 参数adAffectCurrent为删除当前记录

pRecordset->Update();

AfxMessageBox("删除成功!");

}

catch (_com_error *e)

{

AfxMessageBox("异常发生!");

AfxMessageBox(e->ErrorMessage());

}

//遍历结果集

pRecordset->MoveFirst();

while(!pRecordset->adoEOF)

{

_variant_t var = pRecordset->Fields->GetItem("AGE")->Value;//username

//结果有待处理

AfxMessageBox((_bstr_t)var);

pRecordset->MoveNext();

}

//关闭查询器

if(pRecordset->State)

{

pRecordset->Close();

pRecordset = NULL;

}

//关闭数据库连接

if(pConnection->State)

162094275_1_20190525084454316

{

pConnection->Close();

pConnection = NULL;

}

AfxMessageBox("添加成功");

}

catch(_com_error &e)

{

AfxMessageBox(e.ErrorMessage());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值