Aks
Aks

Reputation: 5236

Perl DBI: Connect to SQL Server that uses Windows Authentication

I'm using perl DBI to connect to a SQL Server. This server uses windows authentication. How do I pass on this information to the SQL server via DBI?

Upvotes: 2

Views: 9464

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

I assume that "using Windows authentication" implies that your script runs on Windows. Then there is more than one way to do it. You can use ADO instead of ODBC and don't need to create a DSN:

  my $db_instance = ".\\SQLEXPRESS";
  my $db_name     = "AdventureWorks";
  my $db_user     = "sa";
  my $db_pass     = "...";

  my $n = 0;
  for my $cs (
      "DBI:ODBC:Driver={SQL Server};Server=$db_instance;Database=$db_name;UID=$db_user;PWD=$db_pass"
    , "DBI:ODBC:Driver={SQL Server};Server=$db_instance;Database=$db_name;"
    , "DBI:ODBC:DSN=AdvWork;"
    , "DBI:ADO:DSN=AdvWork;"
    , "DBI:ADO:Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=$db_name;Data Source=$db_instance;"
    , "DBI:ADO:Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PWD=$db_pass;Initial Catalog=$db_name;Data Source=$db_instance;"
    , "DBI:ADO:Provider=SQLNCLI10.1;Integrated Security=SSPI;Persist Security Info=False;User ID=\"\";Initial Catalog=$db_name;Data Source=$db_instance;Initial File Name=\"\";Server SPN=\"\";"
  ) {
     my $dbh = DBI->connect($cs) or die("\nCONNECT ERROR:\n$DBI::errstr");
     my $wtf = $cs;
     $wtf =~ s/$db_pass/secret/;
     printf "%2d CS: '%s'\n", ++$n, $wtf;
     $dbh->disconnect();
  }

output:

xpl.pl::Main started using Perl 5.010000 on MSWin32.
will call CnctMSSS - connect to MS SQLServer (SQLExpress)
 1 CS: 'DBI:ODBC:Driver={SQL Server};Server=.\SQLEXPRESS;Database=AdventureWorks;UID=sa;PWD=secret'
 2 CS: 'DBI:ODBC:Driver={SQL Server};Server=.\SQLEXPRESS;Database=AdventureWorks;'
 3 CS: 'DBI:ODBC:DSN=AdvWork;'
 4 CS: 'DBI:ADO:DSN=AdvWork;'
 5 CS: 'DBI:ADO:Provider=SQLNCLI.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Advent
ureWorks;Data Source=.\SQLEXPRESS;'
 6 CS: 'DBI:ADO:Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;PWD=secret;Initial Catalog=Adventur
eWorks;Data Source=.\SQLEXPRESS;'
 7 CS: 'DBI:ADO:Provider=SQLNCLI10.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial C
atalog=AdventureWorks;Data Source=.\SQLEXPRESS;Initial File Name="";Server SPN="";'
CnctMSSS returned 0 [5.858424 secs]
xpl.pl::Main done. (0)

If you compare (1) [based on this contribution] and (2), you see that skipping the user info is enough to connect to the database; some drivers/providers may like the addition of "Integrated Security=SSPI".

Here is a good starting point, if you need help with experimenting with connection strings.

Upvotes: 2

bohica
bohica

Reputation: 5992

I presume you are using DBD::ODBC. Create a data source for SQL Server in the ODBC Administrator which uses Windows authentication then call DBI's connect method without a username and password.

Upvotes: 1

Related Questions