Windows和Linux系统下perl连接SQL Server数据库的方法
Perl通过DBI模块连接SQLServer:Windows下可用DBD::ODBC或DBD::ADO,支持动态连接或系统DSN;Linux下则借助DBD::Sybase和FreeTDS驱动走TDS协议,需配置freetds.conf并设置环境变量。
Perl 连接 Microsoft SQL Server,Windows 和 Linux 环境下的实现方式各有一套。下面直接上几个操作实例,覆盖两种主流平台,代码可以直接拿去用。

Windows 平台
在 Windows 下跑 Perl 脚本连接 SQL Server,推荐走 DBI 这条主线,然后再选两个方便的驱动模块——DBD::ODBC 或 DBD::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 可以避免崩溃。这一点在实际生产环境中很值得留意。
Postman是一套面向API全生命周期的开发与测试平台,可在Mac上发送和调试多种协议请求,管理集合、环境及认证信息,并完成脚本测试、模拟服务、监控、文档发布和团队协作。它适合开发、测试及运维人员使用,提供个人免费方案。
Docker Desktop 是 Docker 官方为 Mac 提供的容器开发环境,集成 Docker Engine、命令行工具、Compose、Build、Kubernetes 与图形化管理界面。
Android Studio 是 Google 面向安卓应用开发提供的官方集成开发环境,基于 IntelliJ IDEA 构建。它整合智能代码编辑、Gradle 构建、界面预览、设备模拟、性能分析、调试测试及 APK 分析等工具。
PixPix 是面向品牌、卖家和内容团队的在线 AI 图片视频创作平台,提供图视频 Agent、无限画布、图片生成、视频生成、自然语言编辑及商品套图、产品精修和带货视频等电商工具。
Postman 是面向 API 全生命周期的开发与协作平台。Windows 桌面版可用于构建和发送 HTTP、GraphQL、gRPC、WebSocket、MQTT 等请求,并提供环境变量、集合、脚本测试、模拟服务、监控、文档及团队工作区等
Docker Desktop 为 Windows 开发者提供集成式容器环境,内置 Docker Engine、命令行工具、Compose、Build 与 Kubernetes 等组件,可在本机完成镜像构建、容器运行、日志查看和资源管理。













