本文将提供一些perl连接Microsoft SQL Server数据库的实例。perl脚本运行在Windows和Linux平台。
Windows平台
如果在Windows平台下运行perl脚本,建议使用依赖DBI的两个模块包,提供标准的数据库接口模块。
DBD::ODBC
DBD::ADO
使用DBD::ODBC
如果选用DBD::ODBC,下面的实例代码将展示如何连接到SQL Server数据库:
复制代码 代码如下:
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_number\n"; 
} 
 
#Close the connection 
$sth->finish(); 
$dbh->disconnect();
你还可以使用预先设置的一个系统DSN来连接。要建立一个系统DSN,可以这样访问控制面板->管理工具->数据源。
使用系统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模块,下面的实例展示如何连接到SQL Server数据库。
复制代码 代码如下:
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_number\n"; 
} 
 
#Close the connection 
$sth->finish(); 
$dbh->disconnect();
Linux平台
如果是在Linux平台下运行perl脚本,连接SQL Server数据库需要使用到DBD::Sybase包。
安装SQL Server支持库
Sybase DBD包依赖FreeTDS驱动程序。
FreeTDS下载地址:www.freetds.org
安装FreeTDS驱动的说明文档参见:http://www.freetds.org/userguide/config.htm
该驱动没有使用到ODBC.
配置数据源
修改freetds.conf文件包括SQL Server数据库信息,如下所示:
复制代码 代码如下:
[SS_MY_DB]
host = 10.0.0.1 # or host name port = 1433
tds version = 7.0
安装Sybase DBD模块
该模块文档参见:http://search.cpan.org/~mewp/DBD-Sybase/Sybase.pm
此外,需要将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, $phone\n"; 
} 
 
#Close the connection 
$sth->finish(); 
undef $sth; # This fixes a segfault bug with certain versions of DBD::Sybase 
$dbh->disconnect();
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
 - 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
 - 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
 - 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
 - 群星《2024好听新歌42》AI调整音效【WAV分轨】
 - 王思雨-《思念陪着鸿雁飞》WAV
 - 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
 - 李健《无时无刻》[WAV+CUE][590M]
 - 陈奕迅《酝酿》[WAV分轨][502M]
 - 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
 - 群星《吉他王(黑胶CD)》[WAV+CUE]
 - 齐秦《穿乐(穿越)》[WAV+CUE]
 - 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
 - 邝美云《邝美云精装歌集》[DSF][1.6G]
 - 吕方《爱一回伤一回》[WAV+CUE][454M]