2013-11-25 195 views
0

Perl非常新。我遇到了試圖讓DBI與SQL Server 2008數據庫通信的問題。Perl DBI SQL SERVER連接性問題

當我嘗試使用ODBC或直接嘗試連接到SQL Servereveb時,出現以下錯誤。

我是新來的Perl,有人可以幫助...感謝

install_driver(MSSQL)失敗:無法找到對象的方法 「set_sql」 通過一攬子 「類:: DBI :: MSSQL」 位於C :/Perl/lib/DBD/MSSQL.pm線79.編譯失敗在(EVAL 19)C需要:/Perl/site/lib/DBI.pm:744線3

use strict; 
use warnings; 
use diagnostics; 
use Class::DBI::Loader; 

use DBI; 

use File::Glob ':glob'; 


my $DBUserName      = "*******"; 
my $DBPassword      = "*******"; 
my $DBName       = "dbi:MSSQL:uat-dbserver1"; 

my $dbh       = ""; 
my $sqlStatement     = ""; 
my $sqlCmd       = ""; 

my @EasySetTableNames   =(); 



$dbh = DBI->connect($DBName, $DBUserName, $DBPassword, 
    { PrintError => 0, AutoCommit => 0}) 
     || die "Database connection creation failed: $DBI::errstr\n"; 

$sqlStatement = "SELECT * from tableA "; 
$sqlCmd = $dbh->prepare($sqlStatement); 
$sqlCmd->execute(); 
@EasySetTableNames = @{$dbh->selectcol_arrayref($sqlStatement)}; 
print "hi"; 

並通過ODBC

#!/usr/bin/perl -w 
use strict; 

use DBI; 

# Replace datasource_name with the name of your data source. 
# Replace database_username and database_password 
# with the SQL Server database username and password. 
my $data_source = "dbi:MSSQL:test"; 
my $user = "test"; 
my $password = "test"; 

# Connect to the data source and get a handle for that connection. 
my $dbh = DBI->connect($data_source, $user, $password) 
    or die "Can't connect to $data_source: $DBI::errstr"; 

# This query generates a result set with one record in it. 
my $sql = "SELECT 1 AS test_col"; 

# Prepare the statement. 
my $sth = $dbh->prepare($sql) 
    or die "Can't prepare statement: $DBI::errstr"; 

# Execute the statement. 
$sth->execute(); 

# Print the column name. 
print "$sth->{NAME}->[0]\n"; 

# Fetch and display the result set value. 
while (my @row = $sth->fetchrow_array) { 
    print "@row\n"; 
} 

# Disconnect the database from the database handle. 
$dbh->disconnect; 

任何幫助你可以提供將是如此讚賞。

+0

Re「DBI是否直接支持SQL SERVER?」這取決於個人DBD。大多數人不知道ODBC。 DBD :: ODBC是明顯的例外。 – ikegami

+0

哼,您使用的DBD :: MSSQL在CPAN上不存在! – ikegami

+0

「Class :: DBI :: MSSQL」的文檔表示在引擎蓋下使用了'DBD :: ODBC'。 – tjd

回答

1

我通常從內部DBI使用ODBC驅動程序,這是我通常會打的SQL Server(2008 R2)

#!/export/appl/pkgs/perl/5.8.4-fiq/bin/perl 
    #!/usr/bin/perl 
    #!/bin/sh 

    use strict; 
    use DBI; 
    use Time::localtime; 
    use Data::Dumper; 

    my $dsn = 'DBI:ODBC:Driver={SQL Server}'; 
    my $host = 'xxx\yyy'; 
    my $database = 'testing'; 
    my $user = 'user'; 
    my $auth = 'password'; 


    my $dbh = DBI->connect("$dsn;Server=$host;Database=$database", $user, $auth) or die "Database connection not made: $DBI::errstr"; 

    my $sql = "EXECUTE database.schema.sproc"; 
    my $stmt = $dbh->prepare($sql); 
    $stmt->execute(); 
    $stmt->finish(); 
    $dbh->disconnect; 
+1

將'; Trusted_Connection = Yes'附加到連接字符串的末尾將使用登錄用戶的憑據連接到MSSQL服務器。優點:無需將證書保存在程序或其配置文件中。缺點:DBA需要保持最新的權限。 (有些人可能會認爲這是他們的工作......) – tjd