Win32::OLE ADO Table Field names

G

goldtech

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 per 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?

#!/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.
 
S

still just me

On Sun, 23 Dec 2007 10:15:38 -0800 (PST), goldtech


It's been a while since I poked around with this... but does this
help?

$rs-> Fields->{Count}; # number of columns for this row
$rs-> Fields( N )->{Name}; # name of the Nth column
$rs-> Fields( N )->{Value}; # value of the Nth column

# extract column names from first record
for my $i ( 0 .. $rs-> Fields->{Count}-1 ) {
$names{ $rs-> Fields(0)->{Name} } = $i;
}
 
G

goldtech

Got it finally:

....
$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.' ';
....

:^)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top