louis xie
louis xie

Reputation: 1422

Using Perl to connect to MS SQL without use of an ODBC DSN

Is there a way to use Perl to connect to a MS SQL database without the use of ODBC? I have a few scripts running in my production environment which I intend to migrate to perl.

Sample SQLCMD used:

sqlcmd -S <dbHostName> -i <input_file> -o <output_file>

Sample BCP used:

bcp <sqlQuery> queryout <output_file> -c -t, -T -S <dbHostName>

The main reason is that I do not know the password to my production database, and I cannot have knowledge of it. Therefore there is no way I can pass in the pass phrase whether in my perl script or via the ODBC setup.

Upvotes: 0

Views: 1699

Answers (1)

tjd
tjd

Reputation: 4104

I notice that your sqlcmd statement does not include -U and -P. This means it is using the logged-in user's credentials to access the database.

To accomplish the same with ODBC & DBI, include Trusted_Connection=True; in the connection string. This is akin to using the "With Windows NT authentication using the network login ID." radio button on the 2nd page of the DSN setup wizard.

For example on my Win 8.1 box, where SQL Server is the name of the relevant driver (not DSN) in my ODBC Data Source Administration interface, I might give the DBI connect method the following string:

dbi:ODBC:Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

In contrast a string using a DSN might look like:

dbi:ODBC:myDSN

FWIW: I find http://connectionstrings.com to be a valuable resource.

Upvotes: 1

Related Questions