操作数据库void CLogin::OnBnClickedButton1()
{
if( m_user.GetWindowTextLength()<=0 || m_password.GetWindowTextLength()<=0 )
{
MessageBox(TEXT("请输入用户名或密码!"), TEXT("错误:用户名或密码为空"), MB_OK | MB_ICONWARNING );
return;
}
if( FALSE==this->ConnectDB() ) return;
TCHAR szUserName[20];
TCHAR szPassword[50];
TCHAR szSql[MAX_PATH];
//获取用户输入的用户名和密码
m_user.GetWindowText( szUserName, 20 );
m_password.GetWindowText( szPassword, 50 );
//创建_RecordsetPtr用来执行数据库操作
_RecordsetPtr pRecordset;
pRecordset.CreateInstance("ADODB.Recordset");
_tcscpy_s( szSql, TEXT("select * from usr where uname='") );
_tcscat_s( szSql, szUserName );
_tcscat_s( szSql, TEXT("' and passwd='") );
_tcscat_s( szSql, szPassword );
_tcscat_s( szSql, TEXT("';") );
pRecordset->Open(_variant_t(szSql), _variant_t ((IDispatch*) m_pConnection,true), adOpenStatic, adLockOptimistic, adCmdText);
int nResult = DLG_RESULT_OK;
if( pRecordset->RecordCount>0 )
{
//MessageBox(TEXT("登录成功!"), szUserName, MB_OK | MB_ICONINFORMATION );
}
else
{
MessageBox(TEXT("用户名或密码错误!"), TEXT("登录失败!"), MB_OK | MB_ICONINFORMATION );
nResult = DLG_RESULT_ERROR;
}
pRecordset->Close();
if( DLG_RESULT_OK==nResult ) Exit( DLG_RESULT_OK );
}