3.4.2. Connecting to the database
To connect to the database, it is necessary to change the Connected property of the TFDConnection component to True or call the Open method. You can use the Open method to pass the username and password as parameters.
A Little Modification
We will replace the standard database connection dialog box in our application and allow users to make three mistakes while entering the authentication information. After three failures, the application will be closed.
To implement it, we will write the following code in the OnCreate event handler of the main data module.
// After three unsuccessful login attempts, we close the application.xLoginCount := 0;xLoginPromptDlg := TLoginPromptForm.Create(Self);while (xLoginCount < MAX_LOGIN_COUNT) and(not FDConnection.Connected) dobegintryif xLoginPromptDlg.ShowModal = mrOK thenFDConnection.Open(xLoginPromptDlg.UserName, xLoginPromptDlg.Password)elsexLoginCount := MAX_LOGIN_COUNT;excepton E: Exception dobeginInc(xLoginCount);Application.ShowException(E);endend;end;xLoginPromptDlg.Free;if not FDConnection.Connected thenHalt;
