When using TADOConnection in Delphi, you may want to control when the login dialog appears. Setting LoginPrompt to True triggers the prompt each time a connection is attempted, which is useful if credentials aren’t stored in the connection string.
Example Usage
Here's an example illustrating conditional use of LoginPrompt:
procedure TfrmMain.FormShow(Sender: TObject);
var
i: integer;
begin
con1.Provider := 'SQLOLEDB.1';
con1.Properties['Application Name'].Value := Application.Title;
with TIniFile.Create(ExtractFileDir(ParamStr(0)) + '\setup.ini') do
begin
con1.Properties['Initial Catalog'].Value := ReadString('database', 'Initial Catalog', '');
con1.Properties['Data Source'].Value := ReadString('database', 'Data Source', '');
if ReadBool('database', 'Integrated Security', false ) then
begin
con1.Properties['Integrated Security'].Value := 'SSPI';
con1.Properties['Persist Security Info'].Value := 'False';
con1.LoginPrompt := False;
end
else
begin
con1.Properties['Persist Security Info'].Value := 'True';
con1.LoginPrompt := true;
end;
end;
end;
procedure TfrmMain.con1Login(Sender: TObject; Username, Password: string);
begin
con1.Properties['User ID'].Value := Username;
con1.Properties['Password'].Value := Password;
end;
In this case, the prompt appears only when credentials are missing, providing flexibility and enhancing security.
Tips
- Conditionally prompt: Use LoginPrompt based on your security needs.
- Avoid hardcoded credentials: Protect sensitive data when LoginPrompt is enabled.
- Consistent user experience: Keep the prompt behavior uniform across your app.
Final Thoughts
Using LoginPrompt wisely can simplify the user experience while ensuring secure access.
For more details about it and much more similar posts, check this page on my blog: TADOConnection: Correct Way to Use LoginPrompt.
Top comments (0)