国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫的方法

2020-06-04 20:31:38
字體:
供稿:網(wǎng)友

本文將提供一些perl連接Microsoft SQL Server數(shù)據(jù)庫的實例。perl腳本運行在Windows和Linux平臺。

Windows平臺

如果在Windows平臺下運行perl腳本,建議使用依賴DBI的兩個模塊包,提供標(biāo)準(zhǔn)的數(shù)據(jù)庫接口模塊。

DBD::ODBC
DBD::ADO

使用DBD::ODBC

如果選用DBD::ODBC,下面的實例代碼將展示如何連接到SQL Server數(shù)據(jù)庫:

代碼如下:
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();

你還可以使用預(yù)先設(shè)置的一個系統(tǒng)DSN來連接。要建立一個系統(tǒng)DSN,可以這樣訪問控制面板->管理工具->數(shù)據(jù)源。

使用系統(tǒng)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數(shù)據(jù)庫。

代碼如下:
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();

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 静宁县| 项城市| 南丰县| 湘乡市| 香港 | 高青县| 宜阳县| 浦北县| 简阳市| 碌曲县| 丰顺县| 迁西县| 上蔡县| 德阳市| 搜索| 绥阳县| 故城县| 茶陵县| 红原县| 鄂州市| 荥阳市| 张掖市| 大关县| 林西县| 崇信县| 黄浦区| 巩义市| 焉耆| 桐柏县| 衡南县| 固原市| 衡阳市| 福泉市| 衢州市| 泰安市| 广东省| 莱州市| 永丰县| 周宁县| 六枝特区| 商南县|