2011-08-08 77 views
3

我正在使用SQL Server驅動程序。但是,這是下面的錯誤,我得到:在PERL中從Windows訪問Microsoft 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 

什麼錯誤(S)我在做什麼?

回答

5

你可以試試這個:

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字符串包含逐字$database$host,而不是它們的內容。

+1

+1 - RaiseError是一個很好的補充! –

1

首先添加使用嚴格和使用警告,因爲您正在引用$ database和$ host之前它們被聲明和分配任何東西。但是,在這種情況下,如果使用「而不是」嚴格的話,就不會在第4行找到問題。然後在聲明$ dsn之前移動其他聲明。刪除'use DBD :: ODBC',因爲它什麼也不做在這種情況下,最後讀到關於ODBC連接字符串的信息(請參閱DBD :: ODBC常見問題解答中的參考資料),您不能在連接字符串中放置一個裸數據庫名稱和主機。 attr2 = value2'其中屬性部分由ODBC部分定義,部分由ODBC驅動程序定義。bvr上面的示例顯示:

相關問題