本帖最后由 houwenjie1989 于 2012-05-12 17:45:06 编辑

解决方案 »

  1.   

    void CLoginDlg::OnOK() 
    {
    // TODO: Add your control notification handler code here
    CString username,password;//字符串来用记录输入的用户名和密码
    GetDlgItem(IDC_USER)->GetWindowText(username);//获取用户名
    GetDlgItem(IDC_PASSWORD)->GetWindowText(password);//获取密码
    if(username.IsEmpty()||password.IsEmpty())//判断用户名或密码是否为空
    {

    MessageBox("密码或用户名不能为空!!!");//若为空,显示"密码或用户名不能为空!!!"
    }
    {
    CString sql;//字符串用来存储sql语句
    CClassManageSysApp *App ;//CWinApp对象的指针 sql.Format("select * from UserInfo where [username] = '%s' and [password] = '%s'",username ,password);//sql语句
    App = (CClassManageSysApp*)AfxGetApp();//返回指向应用程序的单一的CWinApp对象的指针
    App->m_data.m_pRecordset->CursorLocation =  adUseServer ;//设置记录集游标属性
    App->m_data.m_pRecordset->Open((_bstr_t)sql,App->m_data.m_pConnection.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,0);
    //打开记录集
    if(App->m_data.m_pRecordset->RecordCount>0)//判断记录集是否存在记录
    {
    try
    {
    CString str= (char*) (_bstr_t)App->m_data.m_pRecordset->GetFields()->GetItem("username")->Value;//读取记录集中读取数据
    App->m_data.m_pRecordset->Close();//关闭记录集

    }
    catch (...)//捕捉异常,
    {
    MessageBox("用户名或密码不正确!!!");//显示 “用户名或密码不正确!!!”
    App->m_data.m_pRecordset->Close();//关闭记录集
    return ;
    }
    EndDialog(IDOK);//关闭登陆对话框

    }
    else
    {
    App->m_data.m_pRecordset->Close();//关闭记录集
    MessageBox("用户名或密码不正确!!!");//显示 “用户名或密码不正确!!!”
    }

    }
    }