2013-11-25 3 views
0

Perl에 새로 입문되었습니다. DBI가 SQL 서버 2008 DB와 통신하도록하는 데 문제가 있습니다.Perl DBI SQL 서버 연결 문제

ODBC를 직접 사용하려고 할 때 SQL Servereveb에 연결하려고하면 다음과 같은 오류가 발생합니다.

나는 펄에 새로운 오전 누군가가 ... 도와 주실 래요 감사

install_driver (MSSQL)는 실패 : "MSSQL을 클래스 :: DBI ::"패키지를 통해 객체 메소드 "set_sql"를 찾을 수 없습니다 C에서 : /Perl/lib/DBD/MSSQL.pm 라인 79 컴파일에 실패 (평가 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 "SQL Server를 직접 지원합니까?" 그것은 개별 DBD에 달렸습니다. 대부분의 ODBC에 대해 몰라요. DBD :: ODBC는 명백한 예외입니다. – ikegami

+0

흠, 당신이 사용하는 DBD :: MSSQL은 CPAN에 존재하지 않습니다! – ikegami

+0

'Class :: DBI :: MSSQL'에 대한 문서는 후드에서'DBD :: ODBC'를 사용하고 있음을 나타냅니다. – tjd

답변

1

나는 보통 DBI 내에서 ODBC 드라이버를 사용하고 이것이 내가 일반적으로 히트 얼마나 SQL 서버 (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