4,011
社区成员
发帖
与我相关
我的任务
分享
try//连接数据库
{
m_pConnection.CreateInstance(__uuidof(Connection));
_bstr_t strConnect="Provider=OraOLEDB.Oracle.1;User ID='system';Password='tiger';Data Source=orcl;Persist Security Info=False;";
m_pConnection->Open(strConnect,"system","tiger",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(TEXT("数据库连接出错!"));
AfxMessageBox(e.Description());
exit(-1);
}
m_pRecordset.CreateInstance("ADODB.Recordset");//创建Recordset实例
CString strSQL="select * from VISIBILITY"; //连接VISIBILITY表
try
{
m_pRecordset->Open((_variant_t)strSQL,(IDispatch*)theApp.m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
} //m_pConnection我在上一组代码中已经连接上oracle服务器了
catch (_com_error* e)
{
AfxMessageBox(e->ErrorMessage());
}
CString StrSQL3;
CString Time="2018/12/20 19:00";
CString visib="1.3";
StrSQL3.Format("INSERT INTO VISIBILITY(采样时间,能见度) VALUES(#%s#,'%s')","2018/12/20 18:00","3.0");
//我自己写死了一个记录准备插入
try{
m_pRecordset->AddNew(); //新建一条记录
m_pRecordset->PutCollect("采样时间",_variant_t(Time)); //修改每个字段值
m_pRecordset->PutCollect("能见度",_variant_t(visib)); //方法一。用AddNew然后加Update,可以成功插入记录
//(theApp.m_pConnection)->Execute((_bstr_t)StrSQL3, NULL, adCmdText); //方法二。直接用Execute插入,无BUG,却没有在数据库中增加数据
}
catch(_com_error e)
{
MessageBox(e.Description());
return;
}
m_pRecordset->Update(); //更新至数据库