首先我定义了一个基类:
class DataService  
{
public:
DataService();
virtual bool GetPassword(CString *UserName,CString *Password);
virtual ~DataService();};然后又定义了一个类继承上面的类,实现数据库连接:
class DataService_SQLService : public DataService  
{
public:
DataService_SQLService();
virtual bool GetPassword(CString *UserName,CString *Password);
virtual ~DataService_SQLService();};
并实现了虚函数如下:
bool DataService_SQLService::GetPassword(CString *UserName,CString *Password)
{
CoInitialize(NULL);
try
{
_ConnectionPtr conn("ADODB.Connection");
conn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security   Info=False;Initial Catalog=test;Data Source=20081010-1242";
conn->Open("","","",adConnectUnspecified);
_RecordsetPtr rs("ADODB.Recordset");
        CString strSQL;
strSQL.Format("%s'%s'","select * from m_user where username=",UserName->GetBuffer(UserName->GetLength()));
rs=conn->Execute(_bstr_t(strSQL),0,adCmdText);
if(rs->BOF)
{
rs->Close();
conn->Close();
return false;
}
else
{
                     strcpy(Password->GetBuffer(11),_bstr_t(rs->GetCollect("userpassword")));                rs->Close();
    conn->Close();
    Password->ReleaseBuffer();
    return true;
}
}
catch(_com_error &e)
{
CoUninitialize();
::AfxMessageBox(e.ErrorMessage());
return false;
}
}然后实现“确定”按钮如下:
void CCLoginDlg::OnOK() 
{   
// TODO: Add extra validation here
CString UserName;//用户输入的用户名
CString pass;//用户输入的秘密 CEdit *p;
//获得文本框输入的用户名
p=(CEdit *)GetDlgItem(IDC_USERNAME);
p->GetWindowText(UserName);
//m_edtUserName.GetWindowText(UserName);
CString Password;
if(m_ds->GetPassword(&UserName,&Password))
{   
//::AfxMessageBox("获得的用户名对应的在数据库中的秘密");
//::AfxMessageBox(Password);//这里是从数据库查询到的秘密,输出的秘密正确
//获得文本框输入的密码

CEdit *q;
q=(CEdit *)GetDlgItem(IDC_PASSWORD);
    q->GetWindowText(pass);
//::AfxMessageBox("文本框中的密码");
//::AfxMessageBox(pass);//文本框中也能正确获得用户输入的秘密
//m_dtPassword.GetWindowText(UserName);
    if(pass==Password)
{
::AfxMessageBox("登陆成功");
}
else
{
::AfxMessageBox("非法密码");
}
}
else
{
::AfxMessageBox("非法用户名");
}

CDialog::OnOK();
}void CCLoginDlg::OnCancel() 
{
// TODO: Add extra cleanup here
delete m_ds;

CDialog::OnCancel();
}
虽然::AfxMessageBox(Password);输出的结果和::AfxMessageBox(pass)输出的结果是一样的,但是Password==pass却是false.这是怎么回事呢?还有,如果我连续使用了多个::AfxMessageBox语句,就只有前面两个可以输出,后面的都不能输出,例如:::AfxMessageBox("获得的用户名对应的在数据库中的秘密");
      ::AfxMessageBox("文本框中的密码");
      ::AfxMessageBox("123456");
输出结果是:
获得的用户名对应的在数据库中的秘密
文本框中的密码
而123456不会输出,小弟刚开始学,请各位多多指教。

解决方案 »

  1.   

    看看是不是多了空格,试试移除空格再比较,
    pass.Remove(' ');
    Password.Remove(' ');
    AfxMessageBox是模式对话框,搜下"模式对话非模式对话框 区别"就出来了.
      

  2.   

    不是空格的问题,我按你的方法试了,不行,而且我输出pass和Password他们的输出结果都是一样的,但是用if(pass==Password)却不是true。
      
    AfxMessageBox是模式对话框,我不是一次输出的,下面的结果是先输出第一个,按确定之后会输出第二个,但是再按确定之后就不会输出第三个对话框了。
      ::AfxMessageBox("获得的用户名对应的在数据库中的秘密");
      ::AfxMessageBox("文本框中的密码");
      ::AfxMessageBox("123456");
      

  3.   

    ::AfxMessageBox("获得的用户名对应的在数据库中的秘密");
      ::AfxMessageBox("文本框中的密码");
      ::AfxMessageBox("123456");
    这3句是连着写的吗
      

  4.   

    一个是CSTRING 一个是CSTRING*最好用FIND函数试下
      

  5.   


    但是在执行if(pass==Password)的时候Password也是CString类型的啊。只是之前在调用GetPassword函数时,在GetPassword函数内部Password才是CString*类型的啊。
      

  6.   

    “输出pass和Password他们的输出结果都是一样的”,查查有没有不可见字符如\r\n .
      

  7.   

    把上面的AfxMessageBox中的内容写入文件,然后比对一下就应该很清楚了
      

  8.   

    试了下,连续AfxMessageBox的效果是 
    依次弹出,1关了弹2,2关了弹3.....
    不会出现1出现了2,3,4就不弹