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 Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay