How do I connect to a MSSQL database using Perl's DBI module in Windows?
views:
4151answers:
3
+1
A:
Couldn't find this anywhere reliable. Use Perl code similar to
use DBI;
my $dbs = "dbi:ODBC:DRIVER={SQL Server};SERVER={ServerName}";
my ($username, $password) = ('username', 'password');
my $dbh = DBI->connect($dbs, $username, $password);
if (defined($dbh))
{
#write code here
$dbh->disconnect;
}
else
{
print "Error connecting to database: Error $DBI::err - $DBI::errstr\n";
}
culix
2008-10-14 21:45:46
+5
A:
Use DBD::ODBC. If you just create a data source with the Control Panel -> System Management -> ODBC Data Sources -> System Data Source or User Data Source (those are the names as I remember them, but my XP isn't in English, so I can't check), then all you have to do is use the name of that data source in the DBI connect string.
my $dbh = DBI->connect("dbi:ODBC:$dsn", $user, $pwd, \%attr);
The difference between User and System data source is that the latter is usable by any user.
See also: HOW TO: Create a System Data Source Name in Windows XP
bart
2008-10-14 22:27:19