sql-server – 在PERL中从Windows访问Microsoft SQL Server

我正在使用SQL Server驱动程序.但这是我得到的以下错误:

DBI connect('Driver={SQL Server}:$database:$host','cartertest',...) failed:
[Microsoft][ODBC Driver Manager] Invalid connection string attribute (SQL-01S00)
at PERL_SQL_Connect.pl line 15
Can't call method "disconnect" on an undefined value at PERL_SQL_Connect.pl line 16

这是我的代码:

use DBI;
use DBD::ODBC;
#my $dsn = "dbi:SQL Server:$database:$host";
my $dsn = 'DBI:ODBC:Driver={SQL Server}:$database:$host';

my $host = 'amber';    ##This is the server name
my $database = 'assignmentdb';     ##This is the database name
my $user = 'something';         ## Database User Name
my $auth = "something";

#my $dsn = "dbi:SQL Server:$database:$host";

$dbiconnect = DBI->connect($dsn,$user,$auth);   #line 15
$dbiconnect->disconnect();                      #line 16

我犯了什么错误?

最佳答案
你可以试试这个:

use DBI;

my $host     = 'amber';
my $database = 'assignmentdb';
my $user     = 'something';
my $auth     = 'something';

my $dsn = "dbi:ODBC:Driver={SQL Server};Server=$host;Database=$database";
my $dbh = DBI->connect($dsn, $user, $auth, { RaiseError => 1 });

# .... work with the handle

$dbh->disconnect;

请注意,”字符串不会插入您的变量,因此在您的示例中,$dsn字符串包含verbatim $database或$host,而不是其内容.

转载注明原文:sql-server – 在PERL中从Windows访问Microsoft SQL Server - 代码日志