anand
anand

Reputation: 643

Inno setup: connect to sql using windows authentication

can anyone tell me how to connect to SQL 2008 using windows authentication thru inno? Currently I'm using ado connection to connect to SQL from inno, the user needs windows authentication option too. Please suggest.

Upvotes: 2

Views: 2139

Answers (1)

TLama
TLama

Reputation: 76693

It's enough to remove the credential attributes from your connection string (User Id and Password) when we are talking about the example you've linked in your question and include one of the following:

  1. Integrated Security=SSPI;
  2. Trusted_Connection=True;

This answer is based just on this topic, I haven't tested it.

Update:

I don't know if that's what you want to do, but I'll post it here. The following script creates a custom page with radio buttons letting user choose authentication mode and optionally fill the credentials.

Important:

There is no protection against SQL injection for those credential fields!

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Code]
const
  LT_WindowsAuthentication = 0;
  LT_SQLServerAuthentication = 1;
var
  LoginType: Integer;
  UsernameEdit: TNewEdit;
  UsernameLabel: TLabel;
  PasswordEdit: TNewEdit;
  PasswordLabel: TLabel;

procedure OnLoginTypeChange(Sender: TObject);
var
  EditColor: TColor;
  LabelColor: TColor;
begin
  LoginType := TNewRadioButton(Sender).Tag;
  UsernameEdit.Enabled := LoginType = LT_SQLServerAuthentication;
  PasswordEdit.Enabled := LoginType = LT_SQLServerAuthentication;
  case LoginType of
    LT_WindowsAuthentication: 
    begin 
      EditColor := clBtnFace;
      LabelColor := clGray;
    end;
    LT_SQLServerAuthentication: 
    begin
      EditColor := clWindow;
      LabelColor := clBlack;
    end;
  end;
  UsernameEdit.Color := EditColor;  
  PasswordEdit.Color := EditColor;
  UsernameLabel.Font.Color := LabelColor;
  PasswordLabel.Font.Color := LabelColor;
end;

procedure InitializeWizard;
var
  LoginPage: TWizardPage;
begin
  LoginPage := CreateCustomPage(wpWelcome, 'DB Login', 'Choose a login type to continue...');
  with TNewRadioButton.Create(WizardForm) do
  begin
    Parent := LoginPage.Surface;
    Left := 0;
    Top := 0;
    Width := LoginPage.Surface.ClientWidth;
    Checked := True;
    Tag := 0;
    Caption := 'Windows authentication';
    OnClick := @OnLoginTypeChange;    
  end;
  with TNewRadioButton.Create(WizardForm) do
  begin
    Parent := LoginPage.Surface;
    Left := 0;
    Top := 20;
    Width := LoginPage.Surface.ClientWidth;
    Checked := False;
    Tag := 1;
    Caption := 'SQL Server authentication';
    OnClick := @OnLoginTypeChange;
  end;

  UsernameLabel := TLabel.Create(WizardForm);
  with UsernameLabel do
  begin
    Parent := LoginPage.Surface;    
    Left := 12;
    Top := 44;
    Width := 200;
    Font.Color := clGray;
    Font.Style := [fsBold];
    Caption := 'Username';
  end;
  UsernameEdit := TNewEdit.Create(WizardForm);
  with UsernameEdit do
  begin
    Parent := LoginPage.Surface;    
    Left := 12;
    Top := UsernameLabel.Top + UsernameLabel.Height + 6;
    Width := 200;
    Color := clBtnFace;
    Enabled := False;    
  end;

  PasswordLabel := TLabel.Create(WizardForm);
  with PasswordLabel do
  begin
    Parent := LoginPage.Surface;    
    Left := 12;
    Top := UsernameEdit.Top + UsernameEdit.Height + 6;
    Width := 200;
    Font.Color := clGray;
    Font.Style := [fsBold];
    Caption := 'Password';
  end;
  PasswordEdit := TNewEdit.Create(WizardForm);
  with PasswordEdit do
  begin
    Parent := LoginPage.Surface;
    Left := 12;
    Top := PasswordLabel.Top + PasswordLabel.Height + 6;
    Width := 200;
    Color := clBtnFace;
    Enabled := False;    
    PasswordChar := '*';
  end;
end;

// and in your connection event then use
case LoginType of
  LT_WindowsAuthentication: 
    ADOConnection.ConnectionString := 
      'Provider=SQLOLEDB;' +                    // provider
      'Data Source=Default\SQLSERVER;' +        // server name
      'Initial Catalog=Northwind;' +            // default database
      'Integrated Security=SSPI;';              // trusted connection      
  LT_SQLServerAuthentication:
    ADOConnection.ConnectionString := 
      'Provider=SQLOLEDB;' +                    // provider
      'Data Source=Default\SQLSERVER;' +        // server name
      'Initial Catalog=Northwind;' +            // default database
      'User Id=' + UsernameEdit.Text + ';' +    // user name
      'Password=' + PasswordEdit.Text + ';';    // password
end;

Upvotes: 3

Related Questions