記得先安裝DBI跟DBD::ODBC。

#!perl -w

use strict;
use DBI;
use DBD::ODBC;

my $server = 'Server名稱';
my $database = 'DB名稱';
my $userID = '帳號';
my $pwd = '密碼';
# -- 以下這行請務必使用雙引號,不然變數會被當成字串吃進去,然後就掰了。 --
my $DSN = "driver={SQL Server};Server=$server;database=$database;uid=$userID;pwd=$pwd;";
my $dbh = DBI->connect("dbi:ODBC:$DSN") || die "Can't connect: $DBI::errstr\n";

結束連線。
$dbh->disconnect;

SQL語法設定\執行部份。
my $SQLstr = "SQL指令";
my $sth = $dbh->prepare($SQLstr) || die "Can't prepare the SQL statement:" $DBI::errstr\n";
$sth->execute || die "Can't execute the SQL statement:" $DBI::errstr\n";

將資料print出來。
# -- 將欄位名稱印出來 --
print "$sth->{NAME}->[0]";

取值撈資料。
while(my $ref = $sth->fetchrow_hashref())
{
foreach my $field ( keys %{ $ref } )
{
print DbTest "$field: $ref->{ $field }\n";
}
}

結束SQL指令部份。
$sth->finish;


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 chenys 的頭像
    chenys

    更遠

    chenys 發表在 痞客邦 留言(0) 人氣()