商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > Windows和Linux系统下perl连接SQL Server数据库的方法

Windows和Linux系统下perl连接SQL Server数据库的方法

  发布于2026-06-29 阅读(0)

扫一扫,手机访问

Perl 连接 Microsoft SQL Server,Windows 和 Linux 环境下的实现方式各有一套。下面直接上几个操作实例,覆盖两种主流平台,代码可以直接拿去用。

Windows和Linux系统下perl连接SQL Server数据库的方法

Windows 平台

在 Windows 下跑 Perl 脚本连接 SQL Server,推荐走 DBI 这条主线,然后再选两个方便的驱动模块——DBD::ODBCDBD::ADO,它们都提供了标准的数据库接口。

使用 DBD::ODBC

如果选 DBD::ODBC,下面的代码演示了怎么动态构造连接字符串、执行查询并打印结果:

use DBI;

# DBD::ODBC
my $dsn = 'DBI:ODBC:Driver={SQL Server}';
my $host = '10.0.0.1,1433';
my $database = 'my_database';
my $user = 'sa';
my $auth = 's3cr3t';

# Connect via DBD::ODBC by specifying the DSN dynamically.
my $dbh = DBI->connect("$dsn;Server=$host;Database=$database",
    $user,
    $auth,
    { RaiseError => 1, AutoCommit => 1}
) || die "Database connection not made: $DBI::errstr";

#Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees ";
my $sth = $dbh->prepare( $sql );

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

my( $id, $name, $phone_number );

# Bind the results to the local variables
$sth->bind_columns( undef, $id, $name, $phone_number );

#Retrieve values from the result set
while( $sth->fetch() ) {
    print "$id, $name, $phone_numbern";
}

#Close the connection
$sth->finish();
$dbh->disconnect();

当然,也可以提前在系统里配好一个系统 DSN,连接时直接引用就行。配系统 DSN 的路径是:控制面板 → 管理工具 → 数据源 (ODBC)。

用系统 DSN 的话,连接字符串改成这样:

# Connect via DBD::ODBC using a System DSN
my $dbh = DBI->connect("dbi:ODBC:my_system_dsn",
    $user,
    $auth,
    {
        RaiseError => 1,
        AutoCommit => 1
    }
) || die "Database connection not made: $DBI::errstr";

使用 DBD::ADO

如果选用 DBD::ADO 模块,做法也很相似。注意这里用的是 OLEDB 提供程序:

use DBI;

my $host = '10.0.0.1,1433';
my $database = 'my_database';
my $user = 'sa';
my $auth = 's3cr3t';

# DBD::ADO
$dsn = "Provider=sqloledb;Trusted Connection=yes;";
$dsn .= "Server=$host;Database=$database";
my $dbh = DBI->connect("dbi:ADO:$dsn",
    $user,
    $auth,
    { RaiseError => 1, AutoCommit => 1}
) || die "Database connection not made: $DBI::errstr";

#Prepare a SQL statement
my $sql = "SELECT id, name, phone_number FROM employees "; my $sth = $dbh->prepare( $sql );

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

my( $id, $name, $phone_number );

# Bind the results to the local variables
$sth->bind_columns( undef, $id, $name, $phone_number );

#Retrieve values from the result set
while( $sth->fetch() ) {
    print "$id, $name, $phone_numbern";
}

#Close the connection
$sth->finish();
$dbh->disconnect();

Linux 平台

换到 Linux 环境,事情稍有不同。这时需要借助 DBD::Sybase 包,它能通过 FreeTDS 驱动跟 SQL Server 通信。

安装 SQL Server 支持库

DBD::Sybase 依赖 FreeTDS 这个底层驱动。FreeTDS 可以从它的官网 www.freetds.org 下载,安装说明见 用户配置指南。需要注意的是,这个驱动不走 ODBC,而是直接走 TDS 协议。

配置数据源

安装好 FreeTDS 之后,修改 freetds.conf 文件,把 SQL Server 的信息加进去:

[SS_MY_DB]
host = 10.0.0.1   # or host name
port = 1433
tds version = 7.0

安装 Sybase DBD 模块

模块文档可以在 CPAN 上找到。安装前记得把 $SYBASE 环境变量设置成 FreeTDS 的安装路径,比如:

export SYBASE=/usr/local/freetds

使用 Sybase DBI 和 SQL Server DSN 实例

最后是连接和查询的完整示例:

# load the DBI module
use DBI;
use DBD::Sybase;

my $database="my_database";
my $user="sa";
my $auth="s3cr3t";

BEGIN
{
    $ENV{SYBASE} = "/usr/local";
}

# Connect to the SQL Server Database
my $dbh = DBI->connect("dbi:Sybase:server=ss_my_db;database=$database",
    $user,
    $auth
    {RaiseError => 1, AutoCommit => 1}
) || die "Database connection not made: $DBI::errstr";

#Prepare a SQL statement
my $sql = "SELECT id, name, phone_number FROM employees";
my $sth = $dbh->prepare( $sql );

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

my( $id, $name, $phone_number );

# Bind the results to the local variables
$sth->bind_columns( undef, $id, $name, $phone_number );

#Retrieve values from the result set
while( $sth->fetch() ) {    print "$name, $title, $phonen";
}

#Close the connection
$sth->finish();
undef $sth;  # This fixes a segfault bug with certain versions of DBD::Sybase
$dbh->disconnect();

注意代码最后多了一个 undef $sth 的步骤——某些版本的 DBD::Sybase 存在分段错误 bug,提前清理 statement handle 可以避免崩溃。这一点在实际生产环境中很值得留意。

本文转载于:https://www.jb51.net/article/56288.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

产品推荐

热门关注