Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all , i am creating a simple GUI application in MFC for the student database management.

I have created a login form for an already existing project which I was working on.I had to do this since it was told the login form is needed only now.
I have to use an MS-Access database for storing the login names and passwords and then I have to connect my login form to the that database in such a way,if I type the login and password details on editboxes ,if the details are existing in the database it must take to the other form when "Login" button is clicked else it must show a message saying "Its invalid".This project is a VC++ MFC Dialog based application.

please help me out regarding this ..i have attached my code below i have written a select qurey as well please guide me how can i proceed further

What I have tried:

C++
#include "stdafx.h"
#include "kgf.h"
#include "secdlg.h"
#include "thrdlg.h"
#include "string.h"
#include "odbcinst.h"
#include "afxdb.h"



// secdlg dialog

IMPLEMENT_DYNAMIC(secdlg, CDialog)

secdlg::secdlg(CWnd* pParent /*=NULL*/)
	: CDialog(secdlg::IDD, pParent)
	, m_user(_T(""))
	, m_password(_T(""))
{

}

secdlg::~secdlg()
{
}

void secdlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_user);
	DDX_Text(pDX, IDC_EDIT2, m_password);
}


BEGIN_MESSAGE_MAP(secdlg, CDialog)
	ON_BN_CLICKED(IDOK, &secdlg::OnBnClickedOk)
END_MESSAGE_MAP()


// secdlg message handlers

void secdlg::OnBnClickedOk()
{
     UpdateData();
     CDatabase database;
     CString SqlString;
     CString sDsn;
     CString sDriver = L"MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)";
     CString sFile = L"G:\\manju\\wfh\\rock.accdb";
    
     sDsn.Format(L"ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
 TRY{


  database.Open(NULL,false,false,sDsn);
  //CRecordset recset( &database );

  SqlString.Format(_T("SELECT *FROM userdata WHERE UserID = %s and Password =  %s ", m_user,m_password));
  //bool res=recset.Open(CRecordset::forwardOnly,SqlString,CRecordset::readOnly);
  database.ExecuteSQL(SqlString);
  if (m_user == UserID && m_password == Passsword)
  {
     AfxMessageBox (L" Suucessfully connnected");
  }
  else
  {
     AfxMessageBox(L" UserID and Password is wrong!! ");
  }
  database.Close();
 
 }CATCH(CDBException, e)
 {
     AfxMessageBox(L"Database error: "+e->m_strError);

	//AfxMessageBox(L"wrong username password!");
 }
  END_CATCH;
 }
Posted
Updated 30-Jun-20 21:40pm
v2
Comments
Richard MacCutchan 1-Jul-20 3:56am    
What is the question?

1 solution

Don't do it like that!
Two major mistakes there:

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is in C#, but it's pretty obvious to anyone with experience in C++.

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900