调用Recordset的Delete方法为什么会出错?(附详细代码)

Cacar 2007-08-17 02:31:30
就是在ListBox中选择一条,然后用Delete方法将该条记录删除
------------------------------------
int index;
HRESULT hr;
index=((CListBox*)GetDlgItem(IDC_LIST1))->GetCurSel(); ////获取所选记录在listbox中的索引号
((CListBox*)GetDlgItem(IDC_LIST1))->GetText(index,m_record); ////将索引号对应的记录值赋给m_record

m_pRst=m_pConn->Execute("Select * from employees",NULL,adCmdText); ////
hr = m_pRst->MoveFirst();
if(FAILED(hr))
{
MessageBox("失败!");
}
CString str;
while (!m_pRst->rsEOF)
{
str = (char*)(_bstr_t)m_pRst->GetCollect("email");
if (m_record==str)
{
try
{
//m_pRst->Move(2);
m_pRst->Filter = "email = 'SKING'";
hr = m_pRst->Delete(adAffectCurrent);
if(FAILED(hr))
{
MessageBox("失败!");
}
m_pRst->Update();
MessageBox("删除成功!");
}
catch(_com_error e) ////捕捉连接异常
{
CString err;
err.Format("删除纪录失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(err);
return ;
}

break;
}
else
{
m_pRst->MoveNext();
}
}
-------------------
m_Rst和m_Conn是类成员,
声明:_ConnectionPtr m_pConn;
_RecordsetPtr m_pRst;
在InitDialog的时候进行连接
::CoInitialize(NULL);
m_pConn.CreateInstance(__uuidof(Connection));
m_pRst.CreateInstance(__uuidof(Recordset));
m_pConn->ConnectionString="Provider=OraOLEDB.Oracle;UserID=hr;Password=noodles;Data Source=XE_123.4.181.72";////连接字符串
m_pConn->ConnectionTimeout = 10;/////设置连接超时时间
if (FAILED(m_pConn->Open("","hr","noodles",adConnectUnspecified))) ////打开数据库并判断是否成功
{
return FALSE;
}
...全文
393 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cacar 2007-08-18
  • 打赏
  • 举报
回复
知道为什么了,写出来给大家参考~
From MSDN ado connection.execute方法:
---------------------------------------
The returned Recordset object is always a read-only, forward-only cursor. If you need a Recordset object with more functionality, first create a Recordset object with the desired property settings, then use the Recordset object’s Open method to execute the query and return the desired cursor type.
Cacar 2007-08-17
  • 打赏
  • 举报
回复
就是说m_pRecordset->Open(),或者m_pRst=m_pConn->Execute以后,Recordset都得到了要查询的数据集,可以通过Move之类的访问单条纪录,它们之间的差别在哪儿?*_*
Cacar 2007-08-17
  • 打赏
  • 举报
回复
他们的区别在哪里呢?如果单做查询的时候,Recordset,Connection,Command对象都有一些差不多的方法,好像得到的结果也差不多·······敬请指教!!谢谢!!!
shakaqrj 2007-08-17
  • 打赏
  • 举报
回复
1。看我签名
2。我的都是
m_pRecordset->Open((_variant_t)str,_variant_t(m_pConn,true),adOpenStatic,adLockOptimistic,adCmdText);
获得记录集,而不是通过
m_pConn->execute()
Cacar 2007-08-17
  • 打赏
  • 举报
回复
难道删除pRst指向的记录不能用Delete么?
Cacar 2007-08-17
  • 打赏
  • 举报
回复
跟踪代码发现能够找到符合条件的纪录,就是通过pRst的移动将数据库中记录的相应字段和ListBox选中项相比较,相等的情况下执行Delete(),可是执行Delete()的时候就会出现Unhandled Exception,不知道什么原因,希望大家给看看
zaodt 2007-08-17
  • 打赏
  • 举报
回复
可能是当前没有符合条件的记录。

另外,删除一条记录,你可以使用SQL中的Delete语句,这个比较简单。

像CListBox这种类都通过了 SetItemData 函数,你可以把表的主键值存入;

在删除记录时,只要取出主键值,执行 Delete 语句就可以了。
Cacar 2007-08-17
  • 打赏
  • 举报
回复
代码中的帐号具有删除纪录的权限~,ps
Cacar 2007-08-17
  • 打赏
  • 举报
回复
查询什么的都没有问题,也能正确找到要删除的纪录,就是执行到Delete就会出错

4,011

社区成员

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

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