DEV Community

Dejan Dozet
Dejan Dozet

Posted on

TADOConnection: Proper Use of LoginPrompt

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;
Enter fullscreen mode Exit fullscreen mode

In this case, the prompt appears only when credentials are missing, providing flexibility and enhancing security.

Tips

  1. Conditionally prompt: Use LoginPrompt based on your security needs.
  2. Avoid hardcoded credentials: Protect sensitive data when LoginPrompt is enabled.
  3. 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.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs