连接字符串:try
{
//hr = pConnection.CreateInstance(__uuidof(Connection));
hr = pConnection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
pConnection->CursorLocation=adUseClient;
strsql="Provider=SQLOLEDB;Server=pc-201009181941;Database=ms;Uid=sa;Pwd=sa;";//sql sever 2000连接字符串
                            pConnection->Open(_bstr_t(strsql),"","",adModeUnknown);
}
}
catch(_com_error e)
{
CString err;
err.Format("连接数据库失败,\n\r错误:%s",e.Description());
//MessageBox(err);
AfxMessageBox(err);
return false;
}
执行成功没有catch
接下来执行sql语句不行:
UpdateData(true); if(m_user!=""&&m_pass!="")
{
CString str;
str="select * from Mg where 用户名='"+m_user+"' and ";
str=str+"密码='"+m_pass+"'";
_RecordsetPtr m_Recordset;
m_Recordset.CreateInstance(__uuidof(Recordset)); CPJApp *theApp=(CPJApp *)AfxGetApp();
try
{
m_Recordset=theApp->pConnection->Execute((_bstr_t)str,NULL,adCmdText);
if(m_Recordset->adoEOF)
{
theApp->usr_name=m_user;
m_Recordset->Close();
m_Recordset.Release();
// CDialog::OnOK();
}
else
{
MessageBox("用户名或密码错误!","提示",MB_ICONWARNING);
}
}
catch(_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}
}
else
{
MessageBox("用户名或密码不能为空!","提示",MB_ICONWARNING);
}
总是提示用户名或密码错误 很显然是执行sql语句错误    各位请告知  谢谢啦

解决方案 »

  1.   

    if(!m_Recordset->adoEOF)你搞反了吧
      

  2.   

    用if(m_Recordset->EndOfFile!=VARIANT_FALSE)试试。
    或者用theApp->m_Recordset->Open((_bstr_t)str, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)试试。
      

  3.   

    str="select * from Mg where 用户名='"+m_user+"' and ";
    str=str+"密码='"+m_pass+"'";
    这个字符串好像写得不对吧?
      

  4.   

    再聊一个 :
    在VC中的add watch 中当我写一个变量看值是总是报错:ERROR:symbol "str" not found  是怎么回事
    str也确实定义了  用MessageBox(str)也可以看见值
       
      

  5.   


    未载入调试信息。rebuild all一下再调试看。
      

  6.   

    很正确呀   谢谢 
    那为什么当我第二次调用m_Recordset=theApp->pConnection->Execute((_bstr_t)str,NULL,adCmdText);时执行完后  到了catch 了
      

  7.   

    是这个IDispatch error #3029
    能加你的QQ或MSN吗
      

  8.   


    貌似大家的技术问题都不私聊
    你用theApp->m_Recordset->Open((_bstr_t)str, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)试试看先
      

  9.   


    改成
    pConnection.CreateInstance(__uuidof(Connection));
    pConnection->Open(_bstr_t(strsql),"","",NULL);
    if(!m_Recordset->adoEOF)
      

  10.   

    写错了把   pConnection.CreateInstance(__uuidof(Connection));
    pConnection->Open(_bstr_t(strsql),"","",NULL);
    if(!m_Recordset->adoEOF)
    pConnection是全局变量 我在APP中已经初始化了
      

  11.   


    就是把pConnection的初始化代码改一下啊
      

  12.   

    还是不行呀    总感觉不应该是执行SQL语句的问题
      

  13.   

    同时用用theApp->m_Recordset->Open((_bstr_t)str, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)试试
      

  14.   

    m_Reocrdset是局部变量pConnection是全局变量
    我在登陆框中用完m_Recordset后就用close 和release了
    在图书添加对话框中重新初始化RecordsetPtr对象
      

  15.   

    还是不行  出现错误:IDispatch error #
      

  16.   

    // m_Recordset=theApp->pConnection->Execute((_bstr_t)str,NULL,adCmdText);
    m_Recordset->Open((_bstr_t)str, theApp->pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
    if(!m_Recordset->adoEOF)
      

  17.   

    以下是调通的代码:
    //检查以下代码和你的有什么不同
    #import "c:\program files\common files\system\ado\msado15.dll" named_guids rename_namespace("ADOCG") rename("EOF", "EndOfFile") rename("BOF","FirstOfFile")
    using namespace ADOCG;
    #include "icrsint.h"//全局
    _ConnectionPtr m_pCon;
    _RecordsetPtr m_pRs;try
    {
        m_pCon.CreateInstance(__uuidof(Connection));
        m_pCon->ConnectionTimeout = 3;
        if(SUCCEEDED(m_pCon->Open(strConnect/*换成你的连接字串*/, _T(""), _T(""), NULL)))
        {
            m_pRs.CreateInstance(__uuidof(Recordset));
            if(SUCCEEDED(m_pRs->Open(strSQL/*换成你的SQL字串*/, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)))
            {
                //成功处理
            }
        }
    }
    catch(_com_error &e)
    {
    }
      

  18.   

    我的没有:
    using namespace ADOCG;
     m_pCon->ConnectionTimeout = 3;
    if(SUCCEEDED(m_pRs->Open(strSQL/*换成你的SQL字串*/, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText)))
            {
                //成功处理
            }
      

  19.   

    再增加图书的对话框中增加: HRESULT hr;//只应该为0
    hr=m_Recordset->Open((_bstr_t)str, theApp->pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
    if(SUCCEEDED(hr))
    {
    if(!m_Recordset->adoEOF)
    还是执行到hr=m_Recordset->Open((_bstr_t)str, )就跳到catch了  和以往的不同    以前还要执行if(SUCCEEDED(hr))的   先在都不执行了
      

  20.   


    仔细检查一下你的SQL语句有没有拼对(最好在调试的时候拼完了检查),在数据库中的表名列名是否完全匹配。