Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Table Field Names With ADO and Perl

Status
Not open for further replies.

leegold2

Technical User
Oct 10, 2004
116
Hi,

Given code below, I can get tables and run SQL on an ms-access db - this works well. But, I want to get the field names for each column of a table. I'm sure there's an ADO way of doing it. Help appreciated.
Thanks.

Question: How do I get the field names of each column of a table?
[tt]
#!/usr/bin/perl

# use strict;
use Win32::OLE();
$Win32::OLE::Warn=2;

my $conn = Win32::OLE->new("ADODB.Connection");
my $db = 'C:\Folder4\usa.mdb';
$conn->Open('Provider = Microsoft.Jet.OLEDB.4.0; Data Source='.$db);
my $rs= $conn->OpenSchema(20);

$rs->MoveFirst();
while(!$rs->{EOF}){
my $tn= $rs->Fields(2)->Value;
if (grep /^$tn$/i, 'States') {
my $rowcount = $conn->Execute("SELECT COUNT(*) AS ROW_COUNT
FROM " .$tn.'"');
print "$tn : ".$rowcount->Fields('ROW_COUNT')->Value."\n";
}
$rs->MoveNext;

}

__END__

states : 51
DC included.
[/tt]
 
Got it finally:

Code:
...
$oRS = Win32::OLE->new("ADODB.Recordset");
$oRS->{'ActiveConnection'} = $conn;
$oRS->Open('States'); # "States" is the table name
$zztop=$oRS->Fields->Count;
$who  =$oRS->Fields->Item(2)->Name;
print $zztop.' '.$who.' ';
...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top