runtime error,如何解决

yugiant 2009-07-24 03:35:12
vc++通过ado连接oracle,就如下代码,出现runtime error:
CString c_user,c_password,sql;
_variant_t RecordsAffected;

UpdateData(TRUE);
c_user=m_username;
c_password=m_userpsw;
sql.Format("select * from YHB where username='%s' and password='%s'",c_user,c_password);

m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open("Provider=MSDAORA.1;Data Source=yu;","user","user123",adModeUnknown);

m_pRecordset.CreateInstance(__uuidof(Recordset));

m_pRecordset->CursorLocation=adUseClient;

m_pRecordset->Open((_bstr_t)sql,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
就执行到最后一行就出runtime error的错了,字段名肯定没有错,该怎么解决啊,望指教,谢谢
...全文
405 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yugiant 2009-07-26
  • 打赏
  • 举报
回复
问题基本解决,我发现第一次建库类型为varchar2,第二次类型为char,改了以后就不出这个错了
rendao0563 2009-07-24
  • 打赏
  • 举报
回复
如果你没有指定数据库,是要先指定一下数据库的。
rendao0563 2009-07-24
  • 打赏
  • 举报
回复

hr = m_pConnection->Open(_bstr_t(ConnStr), "", "", adModeUnknown);

m_pRecordset = m_pConnection->Execute("select name from sysdatabases", NULL, adCmdText);


你那种用法我还没用过不清楚。我这里是这么用的。
oyljerry 2009-07-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 yugiant 的回复:]
引用 3 楼 happyparrot 的回复:
看返回值啊。再GetLastError()

请问怎么看返回值
[/Quote]
对于ADO等数据库操作,都要用try ... catch来捕获错误

try {
m_pRecordset->Open((_bstr_t)sql,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdTe....
...
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
yugiant 2009-07-24
  • 打赏
  • 举报
回复
m_pRecordset->Open((_bstr_t)sql,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
这一句问题最大,注释掉这一句还没有问题,打开就有问题了
yugiant 2009-07-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happyparrot 的回复:]
看返回值啊。再GetLastError()
[/Quote]
请问怎么看返回值
yugiant 2009-07-24
  • 打赏
  • 举报
回复
还是出runtime error的错啊

[Quote=引用 10 楼 feilinhe 的回复:]
m_pConnection->Open("Provider=MSDAORA.1;Data Source=yu;","user","user123",adModeUnknown);
改成
m_pConnection->Open("Provider=MSDAORA.1;User ID=user;Password=user123;Data Source=yu;Persist Security Info=False","","",adModeUnknown);
试试
[/Quote]
feilinhe 2009-07-24
  • 打赏
  • 举报
回复
m_pConnection->Open("Provider=MSDAORA.1;Data Source=yu;","user","user123",adModeUnknown);
改成
m_pConnection->Open("Provider=MSDAORA.1;User ID=user;Password=user123;Data Source=yu;Persist Security Info=False","","",adModeUnknown);
试试
yugiant 2009-07-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 rendao0563 的回复:]
  m_pConnection.CreateInstance(__uuidof(Connection));
  m_pConnection->Open("Provider=MSDAORA.1;Data Source=yu;","user","user123",adModeUnknown);

m_pConnection是指针还是对象。
[/Quote]

_ConnectionPtr m_pConnection;
原来create表的时候是指定table space的,这次建表没有,字段名都一样
ljheng 2009-07-24
  • 打赏
  • 举报
回复
m_pRecordset->Open((_bstr_t)sql.GetBuffer(0),m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
将CString转换成所需的_bstr_t类型
快乐鹦鹉 2009-07-24
  • 打赏
  • 举报
回复
表名和字段名都没有改变么?
快乐鹦鹉 2009-07-24
  • 打赏
  • 举报
回复
Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=ems;Data Source=emsdb";
用这样的连接串
yugiant 2009-07-24
  • 打赏
  • 举报
回复
其实我的程序原来是可以执行的,但我把表drop后又create的,就导致这样的错误了
rendao0563 2009-07-24
  • 打赏
  • 举报
回复
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->Open("Provider=MSDAORA.1;Data Source=yu;","user","user123",adModeUnknown);

m_pConnection是指针还是对象。
快乐鹦鹉 2009-07-24
  • 打赏
  • 举报
回复
看返回值啊。再GetLastError()
yugiant 2009-07-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 happyparrot 的回复:]
m_pConnection->Open是否正常?
[/Quote]
我就怀疑这句,但不知道如何看啊
还望指教,谢谢
快乐鹦鹉 2009-07-24
  • 打赏
  • 举报
回复
m_pConnection->Open是否正常?

4,011

社区成员

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

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