发布于2026-06-29 阅读(0)
扫一扫,手机访问
Perl 连接 Microsoft SQL Server,Windows 和 Linux 环境下的实现方式各有一套。下面直接上几个操作实例,覆盖两种主流平台,代码可以直接拿去用。

在 Windows 下跑 Perl 脚本连接 SQL Server,推荐走 DBI 这条主线,然后再选两个方便的驱动模块——DBD::ODBC 或 DBD::ADO,它们都提供了标准的数据库接口。
如果选 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 模块,做法也很相似。注意这里用的是 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 环境,事情稍有不同。这时需要借助 DBD::Sybase 包,它能通过 FreeTDS 驱动跟 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
模块文档可以在 CPAN 上找到。安装前记得把 $SYBASE 环境变量设置成 FreeTDS 的安装路径,比如:
export SYBASE=/usr/local/freetds
最后是连接和查询的完整示例:
# 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 可以避免崩溃。这一点在实际生产环境中很值得留意。
下一篇:Perl函数(子程序)学习笔记
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8