我在用ADO连接数据库时用到MoveLast,MoveNext就报错。原代码是这样的。各们高手帮忙指点。_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;int  iRowCount = 1;HRESULT hr;try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
         hr = m_pConnection->Open("driver={SQLServer};Server=liyelong;DATABASE=gscapp;UID=sa;PWD=","","",adModeUnknown);///连接数据库
}
}
catch(_com_error e)///捕捉异常

CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
} m_pRecordset.CreateInstance("ADODB.Recordset");

_variant_t RecordsAffected;
m_pRecordset =  m_pConnection->Execute("SELECT productname, productcode, partcode, partname, conditioncode, componentname, componentcode FROM ptree_message where productcode = 'SL-40'",&RecordsAffected,adCmdText);取到的这个数据集m_pRecordset是有非空的。但在后面用到MoveLast和MoveNext就会报错在线等答案

解决方案 »

  1.   

    MoveNext问题已经解决,忘了判断是不是在最后
    MoveLast还是报错
      

  2.   

    一样加上判断
    if (!m_pRecordset->adoEOF && NULL != m_pRecordset)
      

  3.   

    我又试过了。如果直接MoveLast是可以的,但如果先MoveFirst再MoveLast就报错了
      

  4.   

    你的记录集是只能前进的那种,也就是说只能从头到尾单向移动,所以调用MoveFirst会出错
      

  5.   

    后面的四种方法用来在 Recordset 中来回移动或滚动:MoveFirst、MoveLast、MoveNext 和 MovePrevious。(其中有的方法对仅向前游标不可用。) 使用 MoveFirst 方法将当前记录位置移动到 Recordset 中的第一个记录。使用 MoveLast 方法将当前记录位置移动到 Recordset 中的最后一个记录。若要使用 MoveFirst 或 MoveLast 方法,Recordset 对象必须支持书签或向后移动游标;否则调用该方法将产生错误。
      

  6.   

    直接从conn.execute返回的记录集不一定支持书签和正确的游标类型
    可以使用adOpenStatic,或者adOpenKeyset类型的游标
    使用recordset的Open方式来取得记录集//   CursorLocation = adUseClient
    //   CursorType = adOpenKeyset
    //   LockType = adLockOptimistic        TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
            pRstAuthors->CursorType = adOpenStatic;        // Use client cursor to enable AbsolutePosition property.
            pRstAuthors->CursorLocation = adUseClient;
            pRstAuthors->Open("select .....", strCnn, adOpenStatic, 
                adLockOptimistic, adCmdText);
      

  7.   

    使用 Connection 对象的 Execute 方法可以执行在指定连接的 CommandText 参数中传递给该方法的查询。如果 CommandText 参数指定按行返回的查询,那么执行产生的任何结果都将存储在新的 Recordset 对象中。如果此命令不用于返回结果(如 SQL UPDATE 查询),则只要指定了 adExecuteNoRecords 选项,提供者就将返回 Nothing;否则,Execute 将返回已关闭的 Recordset。返回的 Recordset 对象始终是只读的、仅向前的游标。如果需要具有更多功能的 Recordset 对象,应先创建具有所需属性设置的 Recordset 对象,然后使用 Recordset 对象的 Open 方法执行查询并返回所需的游标类型。
      

  8.   

    游标类型不对,ForwardOnly不能用MoveFirst
    m_pRecordset->Open((_bstr_t)strSQL, 
    m_pConnection.GetInterfacePtr(),
    adOpenDynamic,  //指定游标类型
    adLockOptimistic,
    adCmdText);
    //可以查看“ADO程序员手册”