使用DBI连接到SQL Server

时间:2016-09-13 08:44:42

标签: sql-server perl dbi

我正在尝试使用Perl Script连接到MS SQL服务器。这是守则。

#!/usr/bin/perl
use strict;
use warnings;

use DBI;

#my $dbfile = "sample.db";

my $dsn      = "dbi:ODBC:SQLServer:dpnsql";
my $user     = "xxx";
my $password = "******";
my $dbh = DBI->connect($dsn, $user, $password, 
{
   PrintError       => 0,
   RaiseError       => 1,
   AutoCommit       => 1,
   FetchHashKeyName => 'NAME_lc',
}
);


$dbh->disconnect;

但我收到的错误如下 enter image description here

请帮我解决这个问题。任何新代码也表示赞赏。

TIA

1 个答案:

答案 0 :(得分:2)

错误表示找不到数据源名称。

这意味着您的dsn不正确。

如果您正在使用x64服务器,请记住x86和x64应用程序有不同的ODBC设置。 [见:https://stackoverflow.com/a/5034297/257635]

使用正确的DSN尝试以下语法。

my $dbh = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=<IP>;UID=$user;PWD=$password",
    {
       PrintError       => 0,
       RaiseError       => 1,
       AutoCommit       => 1,
       FetchHashKeyName => 'NAME_lc',
    }
);