#!c:\perl\bin\perl

use strict;
use warnings;
use DBI;

	my $dbh = DBI->connect("DBI:ODBC:MyDatabase")
		|| die "could not connect to ODBC:MyDatabase:\n$DBI::errstr\n";

	my $stmt = "select * from NameAddress;";

  	my $sth = $dbh->prepare($stmt);
	$sth ||
		die "could not prepare ($stmt):\n$DBI::errstr\n";	

	my $rv = $sth->execute
		|| die "could not execute ($stmt):\n$DBI::errstr\n";

    while (my $row_ref = $sth->fetchrow_arrayref) {
        print "$_\t" for (@ { $row_ref } );
        print "\n";
        }

    $sth->finish;
	$dbh->disconnect
		|| die "could not disconnect to ODBC:MyDatabase:\n$DBI::errstr\n";

__END__

=head1 NAME

Accessing MS Acces Databases from Perl.

=head1 SYNOPSIS

 use strict;
 use warnings;
 use DBI;

  my $dbh = DBI->connect("DBI:ODBC:MyDatabase")
    || die "could not connect to ODBC:MyDatabase:\n$DBI::errstr\n";

  my $stmt = "select * from NameAddress;";

  my $sth = $dbh->prepare($stmt);
  $sth || die "could not prepare ($stmt):\n$DBI::errstr\n";	

  my $rv = $sth->execute
    || die "could not execute ($stmt):\n$DBI::errstr\n";

  while (my $row_ref = $sth->fetchrow_arrayref) {
    print "$_\t" for (@ { $row_ref } );
    print "\n";
    }

  $sth->finish;
  $dbh->disconnect
    || die "could not disconnect to ODBC:MyDatabase:\n$DBI::errstr\n";


=head1 DESCRIPTION

=head2 Set up the ODBC Data Source

=over 4

=item Control Panel -> Administractive Tools

>From My Computer or Windows Explorer, navagate to
Asministrative tools.

=item Data Sources (ODBC)

Open ODBC Data Source Administrator. Go to B<System DSN>
(System DSN describes data sources that can be seen
by all users logged on to your machine.)

=item ADD a data source

Hit B<Add>. You may either B<Create> or B<Select> a database
here. Name the data source B<MyDatabase> for this
test.

=item Populate the Database with MS Access

Open MS Access; create a table B<NameAddress> (for use by
this test program); create several fields - any type/number;
enter a few records.

=back

=head2 Test the Perl script

This script should print your entries. If you have
real data you want to access, create an ODBC Data Source
for it, and modify the connect statement in this
script:

 my $dbh = DBI->connect("DBI:ODBC:PUT_YOUR_DATA_SOURCE_HERE")

And the select statement:

 my $stmt = "select * from YOUR_TABLE_NAME;";

=head1 WHAT'S NEXT?

Go for it! Try other SQL statements. Have fun!

=head1 AUTHOR

Beau E. Cox <beau@beaucox.com>.

=cut


