首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

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

2012-01-07 
调用Recordset的Delete方法为什么会出错?(附详细代码)就是在ListBox中选择一条,然后用Delete方法将该条记

调用Recordset的Delete方法为什么会出错?(附详细代码)
就是在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;
}

[解决办法]
可能是当前没有符合条件的记录。

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

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

在删除记录时,只要取出主键值,执行 Delete 语句就可以了。
[解决办法]
1。看我签名
2。我的都是
m_pRecordset-> Open((_variant_t)str,_variant_t(m_pConn,true),adOpenStatic,adLockOptimistic,adCmdText);
获得记录集,而不是通过
m_pConn-> execute()

热点排行