void CLogin::OnLogin() 
{
// TODO: Add your control notification handler code here ::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tb_person;Data Source=.";
m_pConnection->Open(strConnect,"","",adModeUnknown);
AfxMessageBox("连接成功");
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
} CString SQLStr;
SQLStr.Format(_T("select * from Tab_User where UserName='%s' and PassWord='%s'"),m_name,m_password);
_bstr_t vSQL=_bstr_t((LPCTSTR)SQLStr); m_pRecordset.CreateInstance(_uuidof(Recordset));
m_pRecordset->CursorLocation=adUseClient;
m_pRecordset->Open(vSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
int i=m_pRecordset->GetRecordCount();
if(i==0)
{
MessageBox("登录失败","",MB_OK);
}
else
MessageBox("登录成功","",MB_OK);
}

解决方案 »

  1.   

    有的数据库驱动不支持,GetRecordCount不一定能得到结果。
      

  2.   

    CString SQLStr;
        SQLStr.Format(_T("select COUNT(*) from Tab_User where UserName='%s' and PassWord='%s'"),m_name,m_password);
        _bstr_t vSQL=_bstr_t((LPCTSTR)SQLStr);    m_pRecordset.CreateInstance(_uuidof(Recordset));
        m_pRecordset->CursorLocation=adUseClient;
        m_pRecordset->Open(vSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
        long i=m_pRecordset->GetCollect((long)0);
        if(i==0)
        {
            MessageBox("登录失败","",MB_OK);
        }
        else
            MessageBox("登录成功","",MB_OK);