为什么在使用ADO访问数据时,使用MovePrevious()时出错??(附代码)

ilovenet 2004-03-29 08:47:10
是一个对话框程序,我的代码如下:
在AdoApp下面建立一个_pConnection对象并初始化:
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=fxybdbo.mdb","","",adModeUnknown);///连接数据库
}
}
catch(_com_error e)///捕捉异常
{
errormessage.Format("连接数据库失败!%s",e.ErrorMessage());
return FALSE;
}

在AdoDlg下面建立Recordset对象,
_bstr_t strSQL("Select * from department");
_RecordsetPtr DeparSet;
DeparSet.CreateInstance(__uuidof(Recordset));
DeparSet=theApp.m_pConnection->Execute(strSQL,NULL,adCmdText);

我在使用中,发现只有使用MoveNext,如果使用MovePrevious()就报错,听说是游标设置的问题,可是不太清楚怎么修改过来,盼望指点~~~~~~
...全文
108 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rifnxa 2004-03-29
  • 打赏
  • 举报
回复
接數据庫之前要CoInitialize(NULL);
yonghengdizhen 2004-03-29
  • 打赏
  • 举报
回复
选择合适的游标和锁组合
SystemProgram 2004-03-29
  • 打赏
  • 举报
回复
选择正确的打开方式
spwnihao 2004-03-29
  • 打赏
  • 举报
回复
光标问题,最好使用动态光标
DeparSet=theApp.m_pConnection->Execute(strSQL,NULL,adCmdText);
修改
DeparSet->Open(bstrSQL,(IDispatch*)theApp.m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
ilovenet 2004-03-29
  • 打赏
  • 举报
回复
不是第一条记录,我把DebugXP,t...Key的代码带回去试试,如果成功,立马给分~~~~~~~

谢谢
taianmonkey 2004-03-29
  • 打赏
  • 举报
回复
TCHAR chDataSource[MAX_PATH];
TCHAR errMsg[MAX_PATH],FileName[MAX_PATH];
CString strSqlSentence;

GetModuleFileName(NULL,FileName, MAX_PATH);//获取模块的名称

(_tcsrchr(FileName, '\\'))[1] = 0;//获取模块的绝对路径

lstrcat(FileName,_T("database\\ICCard.mdb"));//填写表的名称

lstrcpy(chDataSource, FileName);

IADORecordBinding *picRs = NULL;
_RecordsetPtr pRs("ADODB.Recordset");
_ConnectionPtr pConn("ADODB.Connection" );

pConn->ConnectionString = chDataSource;
pConn->Provider = _T("Microsoft.Jet.OLEDB.4.0");

strSqlSentence = _T("select * from deal_info");

CString strSql;
//初始化查询处理语句
strSql = strSql +
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source = " +
chDataSource +
";Persist Security Info=False;Jet OLEDB:Database Password=cd;";

try
{
pConn->Open((_bstr_t)strSql, "", "", adModeUnknown);

pRs->QueryInterface(
__uuidof(IADORecordBinding), (LPVOID*)&picRs);

pRs->Open( (_variant_t)strSqlSentence, // 查询DemoTable表中所有字段
pConn.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch (_com_error &e)
{
sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
AfxMessageBox(errMsg);
return;
}

try
{
pRs->MoveNext();
pRs->Close();
pConn->Close();
}
catch (_com_error &e)
{
sprintf(errMsg,_T("Code meaning = %s\n"), (char*) e.ErrorMessage());
AfxMessageBox(errMsg);
return ;
}
DebugXP 2004-03-29
  • 打赏
  • 举报
回复

_bstr_t strSQL("Select * from department");
_RecordsetPtr DeparSet;
DeparSet.CreateInstance(__uuidof(Recordset));
DeparSet->CursorLocation = adUseClient;
DeparSet->CursorType=adOpenStatic;
DeparSet=theApp.m_pConnection->Execute(strSQL,NULL,adCmdText);
striking 2004-03-29
  • 打赏
  • 举报
回复
因为指针当前已经指在你的记录集的第一条记录, 应该先判断一下是否是首条记录.

4,012

社区成员

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

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