2012年5月23日 星期三

Perl script and MSSQL DB

Solution 1:
use Win32::ODBC;
$DSN = "DSN=[my_DSN];UID=[DB_account];PWD=[DB_password];";   # please mind the red part
$DBH = new Win32::ODBC($DSN) or die("Can't connect to SQL Server");
$sql_command = "select * from table";
$DBH->sql($sql_command);
while ($DBH->FetchRow()) {
  @result = $DBH->Data();
  print @result;
}
$DBH->Close();

Solution 2:
use DBI;

my $dbs = "dbi:ODBC:DRIVER={SQL Server};SERVER={[DB_server]}";
my ($username, $password) = (' [DB_account] ', ' [DB_password] ');
my $dbh = DBI->connect($dbs, $username, $password);
$sql_command = "select * from table";
$sth = $dbh->prepare($sql_command);
$sth->execute();
while ( @result = $sth->fetchrow_array ) {
  print @result;
}
$dbh->disconnect;

沒有留言:

張貼留言