I would like my Perl script to make use of ODBC connection string for connecting to DB. This works fine with code below. However I also would like it to use username+password which i have joined in to the ODBC connection. I'd rather not supply these from inside the script.

Anybody understand how to accomplish this?

use DBI;

my $strConn = "dbi:ODBC:MyDB";
my $username = "username";
my $password = "password";

# Does work
$dbh = DBI->connect( $strConn, $username, $password, { PrintError => 1, RaiseError => 1 } );

# Does not work
#$dbh = DBI->connect( $strConn, undef, undef, { PrintError => 1, RaiseError => 1 } );

if ($dbh)
   print "OK\n";
} else {
   print "FAIL\n";

Note: DB should have password set (blanking pwd no option).

Operating-system is Home windows 2008 R2 (64-bit). The ODBC connection is determined in System DSN as 32-bit. Perl-version is 32-bit. Connection is performed to MSSQL 2008 R2.

Presuming you're using Home windows you could utilize the Win32::ODBC module.

use Win32::ODBC;

my $dbh = new Win32::ODBC("odbc_connection_name");    

if ($dbh)
   print "OK\n";
} else {
   print "FAIL\n";

How does one like the account information be passed in?

Here is a command-line version.

use 5.010;
use strict;
use warnings;
use Getopt::Long qw<GetOptions>;

GetOptions( \my %options, qw<user|u password|pwd|p> );
# Non-option arguments will be left on @ARGV
# this script also accepts script.pl [USER] [PASSWORD]
usage( 'User not set!' )     unless ( $options{user} //= shift );
usage( 'Password not set!' ) unless ( $options{password} //= shift );

    = DBI->connect( 
    , @options{ qw<user password> }
    , { PrintError => 1, RaiseError => 1 } 


    = DBI->connect( join( 
    , 'DBI:ODBC:driver={SQL Server}'
    , "Server=$SQL_SERVER"
    , "Database=$SQL_DATABASE"
    , "UID=$option{user}"
    , "PWD=$option{password}"

Check it out with only the very first argument with no 'undef, undef' for that user/passwd. The DBD::ODBC FAQ appears to point you don't require the other two arguments.

You have to understand that for historic reasons DBD::ODBC attempts to call SQLConnect before SQLDriverConnect when the connection string doesn't include DSN=. The ODBC call SQLConnect takes 3 arguments dsn title, account information. It's not obvious in the ODBC documentation if passing NULL (in C) for that account information suggests they aren't for use or otherwise. However, should you specify your DBI connect first argument as dbi:ODBC:DSN=mydsn and omit the username/password arguments for connecting (or pass them as undef) DBD::ODBC calls SQLDriverConnect and also the ODBC driver will find other DSN particulars out of your DSN. However, I have not attempted this and I am slightly concerned the phone call to SQLDriverConnect in DBD::ODBC passes SQL_DRIVER_NOPROMPT because the only argument and doesn't include SQL_DRIVER_COMPLETE. By trying the above mentioned and it doesn't work try altering the phone call to SQLDriverConnect to become SQL_DRIVER_NOPROMPTSQL_DRIVER_COMPLETE and recompile DBD::ODBC. If the them works RT it on rt.cpan.org or answer here and I'll take a look at altering it.

UPDATE 4-November-11 I added an odbc_driver_complete attribute to DBD::ODBC in 1.32_2 dev release. I am not recommending it fixes any problem the OP had however it adds new functionality.