我用 _Recordsetptr 查询数据,是登陆查询,我需要查询帐号和密码是不是错的,用了如下代码
我的帐号是 1111 在m_name 文本框里输入 1111 那么结果就没问题,但是如果输入错误的话那么数据库里肯定就没这记录了
问题是我做了判断,提示帐号错误,但是一到了这行 _variant_t vcount = m_Precordset->GetCollect(Index);
他直接抛出异常,所以下面的代码根本不执行,提示的是异常,而不是帐号错误这提示框,我在网上查了很多种方法,结果一样,只要是输入正确,那么就OK,一旦输入记录里没有的帐号,就抛出异常了
try
{
    UpdateData(true);
    m_Precordset.CreateInstance(__uuidof(Recordset));
    CString strSQL;
    strSQL.Format("select * from Landing where ID ='%s",m_name+"'");
    m_Precordset->Open(_bstr_t(strSQL),m_Pconnction.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
             _variant_t Index = (long)0;
     _variant_t vcount = m_Precordset->GetCollect(Index);
int count = vcount.lVal;
if ( count == 0 )
{
AfxMessageBox("帐号错误!");
}
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("警告:打开数据库发生异常。\r\n错误信息:%s",e.ErrorMessage());
        AfxMessageBox(errormessage);
}

解决方案 »

  1.   

    try 

        UpdateData(true); 
        m_Precordset.CreateInstance(__uuidof(Recordset)); 
        CString strSQL; 
        strSQL.Format("select * from Landing where ID ='%s",m_name+"'"); 
        m_Precordset->Open(_bstr_t(strSQL),m_Pconnction.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText); 
    if ( (m_Precordset->BOF)&&(m_Precordset->adoEOF) ) 

    AfxMessageBox("帐号错误!"); 

    else
    {
                
        _variant_t vcount = m_Precordset->GetCollect("字段名"); 
    }

    catch (_com_error e) 

    CString errormessage; 
    errormessage.Format("警告:打开数据库发生异常。\r\n错误信息:%s",e.ErrorMessage()); 
            AfxMessageBox(errormessage); 
    }
    catch(...)
    {
    }