TADOConnection – Correct way to use LoginPrompt

I never really understand LoginPrompt := true property in TADOConnection class untill today.

Correct way to use it is like this:

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;

  

This code reads some properties needed by connection string from an INI file. Then based on the value of Integrated Security property that it finds in the file it decides if it should use integrated security (Windows) or SQL Server security to connect to the db. If it uses SQL Server security then OnLogon event will be called upon calling Open method on connection. But before that user will be prompted to enter username and password for DB. You can set these properties then in that event.

It is important to use Properties collection and set the value to property that is needed to be initialized not the connection string itself! Because simply StringReplace will not work!

Share on: