只有在正确输入用户命和密码的条件下才可运行,请问高手们,我该怎么做,同时,用户及密码信息怎么管理?怎么加密?需要用到数据库吗?

解决方案 »

  1.   

    用户比较少就直接在代码里写上就可以了,如果比较多而且是动态的最好用数据库你可以用模式对话框控制,如果密码与用户名都正确,就OnOK(){},否则的话就是UpDateData(false)让重新输入用户名密码
      

  2.   

    BOOL CYourApp::InitInstance()
    {
    //some code
             //登陆系统
    CString name;
    CLoginDlg theDlg;
    if (IDOK == theDlg.DoModal()) name = theDlg.m_name;
    else return FALSE;
    //some code
    }BOOL CLoginDlg::OnInitDialog() 
    {
    CDialog::OnInitDialog(); HRESULT hr;
    try {
    hr = pCon.CreateInstance(__uuidof(Connection));///创建Connection对象
    if(SUCCEEDED(hr)) 
    {
    TCHAR szPath[MAX_PATH];
    GetModuleFileName(NULL, szPath, sizeof(szPath) / sizeof(TCHAR));
    if (_tcsrchr(szPath, _T('\\')) != NULL)
    {
    *_tcsrchr(szPath, _T('\\')) = _T('\0');
    }
    CString s;
    s.Format(_T("File Name=%s\\xxxxx.udl"), szPath);
    pCon->ConnectionTimeout=5;
    pCon->ConnectionString = (_bstr_t)s;
    hr = pCon->Open("","","",adConnectUnspecified);
    }
    }
    catch(_com_error e)///捕捉异常 
    {
    CString errormessage; 
    errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage()); 
    AfxMessageBox(errormessage);///显示错误信息 
    }
    pRs.CreateInstance(__uuidof(Recordset)); _variant_t vFieldValue;
    CString strFieldValue;
    CString strSQL = "select name from login order by ID";
    //创建并打开记录集对象
    try{
    pRs = pCon->Execute(_bstr_t(strSQL), NULL, adCmdText);
    while(!pRs->adoEOF)
    {
    vFieldValue = pRs->GetCollect("name");
    strFieldValue = (char*)_bstr_t(vFieldValue);
    m_ctrlName.AddString(strFieldValue);
    pRs->MoveNext();
    }
    }
    catch(_com_error e)
    {
    CString errormessage; 
    errormessage.Format("读取数据库失败!\r\n错误信息:%s",e.ErrorMessage()); 
      AfxMessageBox(errormessage);//显示错误信息
    }
    if (m_ctrlName.GetCount()) m_ctrlName.SetCurSel(0);
    }void CLoginDlg::OnOK() 
    {
    // TODO: Add extra validation here
    //初始化环境
    UpdateData();
    GetDlgItemText(IDC_COMBO, m_name); _variant_t vFieldValue;
    CString strFieldValue;
    CString strSQL = "select * from login where name=\'" + m_name + "\'";
    //创建并打开记录集对象
    try{
    pRs = pCon->Execute(_bstr_t(strSQL), NULL, adCmdText);
    if (!pRs->adoEOF)
    {
    vFieldValue = pRs->GetCollect("password");
    strFieldValue = (char*)_bstr_t(vFieldValue);
    if (strFieldValue==m_password) m_pass = TRUE;
    }
    }
    catch(_com_error e)
    {
    CString errormessage; 
    errormessage.Format("读取数据库失败!\r\n错误信息:%s",e.ErrorMessage()); 
      AfxMessageBox(errormessage);//显示错误信息
    }
    if (!m_pass)
    {
    AfxMessageBox("用户名或密码错误!\n  请重新输入!");
    m_password = _T("");
    UpdateData(FALSE);
    return;
    }
    CDialog::OnOK();
    }void CLoginDlg::OnDestroy() 
    {
    CDialog::OnDestroy();

    // TODO: Add your message handler code here
    //关闭记录和连接
    if(pCon->State) pCon->Close();
    if(pRs->State) pRs->Close();
    pRs.Release();
    pCon.Release();
    }
      

  3.   

    需要的话密码存进数据库可以先HASH一下