【问题标题】:Connect to SQL Server using DBI使用 DBI 连接到 SQL Server
【发布时间】:2017-01-20 19:15:07
【问题描述】:

我正在尝试使用 Perl 脚本连接到 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;

但我收到如下错误

请在这个问题上帮助我。任何新代码也值得赞赏。

TIA

【问题讨论】:

  • 请不要将文字作为图片发布。它很难阅读,也无法复制和粘贴。您还应该记住我们的许多盲人订阅者。
  • @Borodin:我并非完全失明(85%),但我完全同意你的看法 :)

标签: sql-server perl dbi


【解决方案1】:

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

这意味着您的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',
    }
);

【讨论】:

  • 您能建议我获得正确 DSN 的正确方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 2011-06-19
相关资源
最近更新 更多