我在用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就会报错在线等答案
_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就会报错在线等答案
MoveLast还是报错
if (!m_pRecordset->adoEOF && NULL != m_pRecordset)
可以使用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);
m_pRecordset->Open((_bstr_t)strSQL,
m_pConnection.GetInterfacePtr(),
adOpenDynamic, //指定游标类型
adLockOptimistic,
adCmdText);
//可以查看“ADO程序员手册”