使用ADO连接Oracle81数据库时在AddNew时提示数据集不支持更新的问题???请各位大侠指点迷津在线等

xmndragon 2004-07-25 12:02:16
连接函数:
//////////////////////////////////////////////////////////////////////////
// 数据库连接函数。
// ip:数据库ip地址、uid:用户名、 pwd:密码 、source库名
//////////////////////////////////////////////////////////////////////////
BOOL CGatherDoc::ConnectToDB(CString ip,CString uid,CString pwd,CString source)
{
HRESULT hr = S_OK;

if(m_pConnection!=NULL)
{
m_pRecordset=NULL;
m_pConnection->Close();
m_pConnection=NULL;
}
_bstr_t bstrConnectString;
_bstr_t bstrUserID;
_bstr_t bstrPassword;
CString temp;
::CoInitialize(NULL);
try
{
// NOTE: In the following lines, provide your own
// connection string to pubs and user ID as indicated:
// Initialize variables Provider=SQLOLEDB; Data Source=%s,1433;Persist Security Info=False;Initial Catalog=DATADB
//server=//=; Initial Catalog=DATADB
temp.Format(_T("Provider=MSDAORA.1;Data Source=sode_%s;Data Source=%s"),ip,source);
bstrConnectString = temp;
bstrUserID = uid;
bstrPassword = pwd;

// Create an instance of ADOConnection:
if(SUCCEEDED(hr))
hr = m_pConnection.CreateInstance("ADODB.Connection");

// Open the connection:
if(SUCCEEDED(hr))
hr = m_pConnection->Open(bstrConnectString, bstrUserID, bstrPassword,adOptionUnspecified);

// Create an instance of a recordset:
if(SUCCEEDED(hr))
hr = m_pRecordset.CreateInstance(__uuidof(Recordset), NULL);

// When you call GetRecordSet you get a pointer to this recordset:
return SUCCEEDED(hr);
}
catch(_com_error &err)
{
TCHAR szBuf[2056];
_tcscpy(szBuf, _T(""));
_tcscat(szBuf, err.ErrorMessage());
_bstr_t bstrSource(err.Source());
_bstr_t bstrDesc(err.Description());
_tcscat(szBuf, (char*)bstrSource);
_tcscat(szBuf, (char*)bstrDesc);
Message(_T(szBuf));
return FALSE;
}
}
游标OPEN函数:
//////////////////////////////////////////////////////////////////////////
// 数据库查询函数。
// str:SQL语句
// 返回: TRUE 成功 FALSE 查询过程有错误
//////////////////////////////////////////////////////////////////////////
BOOL CGatherDoc::OpenRecordset(CString str)
{
HRESULT hr = S_OK;
try
{
// Open that recordset to a valid table:
_bstr_t bstrQuery=str;
_variant_t vQuery(bstrQuery);
// Pass the ADOConnectionPtr to the recordset so that the
// recordset connects to the appropriate database:
_variant_t vDispatch((IDispatch*)m_pConnection);
// Open the recordset:
hr = m_pRecordset->Open(vQuery,
vDispatch,
adOpenDynamic,
adLockOptimistic,
adCmdText);
return TRUE;
}
catch(_com_error &err)
{
TCHAR szBuf[2056];
_tcscpy(szBuf, _T(""));
_tcscat(szBuf, err.ErrorMessage());
_bstr_t bstrSource(err.Source());
_bstr_t bstrDesc(err.Description());
_tcscat(szBuf, (char*)bstrSource);
_tcscat(szBuf, (char*)bstrDesc);
Message(_T(szBuf));
return FALSE;
}
}
数据记录添加处代码:

if(OpenRecordset(SQL))
{
try
{
if(m_pRecordset->adoEOF)
{
......
m_pRecordset->AddNew(&rgf,&rgv);
}
}

}
请各位大侠指点迷津
...全文
194 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
danielzhu 2004-07-25
  • 打赏
  • 举报
回复
学习
Kudeet 2004-07-25
  • 打赏
  • 举报
回复
在打开数据库前加个
pRecordSet->CursorLocation = adUseClient;试

4,011

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧