Tuesday, August 18, 2009

How to Create a dynamic DSN for Win32::ODBC database


#! c:\perl\bin\perl
use Win32::ODBC;

$DriverType = "Microsoft Access Driver (*.mdb)";
$DSN = "Win32 ODBC --MS_Access--";
$Dir = "c:\\perl";
$DBase = "myDB.mdb";

Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType,("DSN=$DSN", "Description=DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD=")) or die "ConfigDSN(): Could not add temporary DSN" . Win32::ODBC::Error();
$db=new Win32::ODBC($DSN) or die "couldn't ODBC $DSN ", Win32::ODBC::Error(), "\n";
$query = "select * from table";
!$db->Sql($query) or die "query $query failed ", $db->Error(), "\n";

while($db->FetchRow())
{
my %Data = $db->DataHash();
foreach my $key(keys(%Data))
{
print $key," -> ",$Data{$key};
}
}

Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN") or die "ConfigDSN(): Could not remove temporary DSN ", Win32::ODBC::Error();

No comments:

Post a Comment