Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Returning primary key fields

9 views
Skip to first unread message

Ulysses Neto

unread,
Dec 3, 2001, 12:38:50 PM12/3/01
to
Hi,

does anybody know how can I get the primary(ies) key(s) field(s) of a
table, using TAdoTable ?

Thanks for your attention.


Brian Bushay TeamB

unread,
Dec 3, 2001, 11:02:15 PM12/3/01
to
> does anybody know how can I get the primary(ies) key(s) field(s) of a
>table, using TAdoTable ?
You can't.
You can use OpenSchema to get primary key information although your Ole Db
Provider needs to support this.

Here is an example

ADOConnection1.OpenSchema(siPrimaryKeys
,VarArrayOf([Null,Null,'YourTblName']),EmptyParam,ADODataSet1);
--
Brian Bushay (TeamB)
Bbu...@NMPLS.com

Ulysses Neto

unread,
Dec 4, 2001, 8:37:09 AM12/4/01
to
Even if a try to use the interface class "_Table" ? Is there any way to get
this class from TADOTable ? Like, I don´t know, "GetInterface" procedure ?

Brian Bushay TeamB <BBu...@Nmpls.com> escreveu nas notícias de
mensagem:j7go0u8lpris6d84d...@4ax.com...

Brian Bushay TeamB

unread,
Dec 4, 2001, 11:28:35 PM12/4/01
to

>Even if a try to use the interface class "_Table" ? Is there any way to get
>this class from TADOTable
Not that I know of.

Thérèse Hanquet

unread,
Dec 5, 2001, 3:10:27 AM12/5/01
to
Hi Ulysses,

> Even if a try to use the interface class "_Table" ? Is there any way
to get
> this class from TADOTable ? Like, I don´t know, "GetInterface"
procedure ?

In fact, TADOTable is based on a ADO Recordset object, not on a ADOX
Table object. The "Table" in its name only indicates it can be a
substitute for a TTable object for programs written for the BDE.
TADOTable, TADOQuery and TADODataSet are all descendants from
TCustomADODataSet, and the interface they provide is _Recordset.

If you import the ADOX type library, you can access the Keys collection
of the Table object and read the column names of the primary key. If you
want an example of using ADOX, have a look at this code by DRS:
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=15292
It shows (notably) how to add a multi-column primary key to a table.
Adding a key will only work with Jet 4.0, but reading it should work
with other providers.

But if you can, it is probably simpler to use OpenSchema as Brian
suggested.

Thérèse

Ulysses Neto

unread,
Dec 5, 2001, 8:14:33 AM12/5/01
to
Brian and Thérèse

Thanks a lot...


Ulysses Neto

unread,
Dec 5, 2001, 8:35:42 AM12/5/01
to
I have discovered one way to get the ADOX table object:

Using the "connection" property of the TADOTable class, it is possible to
get the "_Connection" ADOX object.
With this object we can create a "_Catalog" object using this connection. In
this "_Catalog" object, there is a property named "Tables" that it's
possible to return all tables of the catalog in use, and selecting the
wanted table, its returns an "_Table" object from this table, where I can
get its primary keys.

The source code:

with ADOTable1 do begin
oCatalogo := CoCatalog.Create;
oCatalogo.Set_ActiveConnection(Connection.ConnectionObject);

Result := '';
with oCatalogo.Tables.Item[TableName].Indexes[0] do begin
for n := 0 to Columns.Count-1 do Result := Result + Columns[n].Name +
';';
end;

Result := Copy(Result, 1, Length(Result)-1);

oCatalogo.Set_ActiveConnection(Unassigned);
oCatalogo := nil;
end;


Thérèse Hanquet

unread,
Dec 5, 2001, 9:12:17 AM12/5/01
to
Hi Ulysses,

> Using the "connection" property of the TADOTable class, it is possible
to
> get the "_Connection" ADOX object.
> With this object we can create a "_Catalog" object using this
connection.

Yes, you can use any ADO object connected to your database, or even the
underlying _Connection object of the TADOConnection itself if you have
one for several objects.
You can also establish the connection on basis of the ConnectionString,
using the _Set_ActiveConnection method.

> with oCatalogo.Tables.Item[TableName].Indexes[0] do begin
> for n := 0 to Columns.Count-1 do Result := Result +
Columns[n].Name +
> ';';

Unless you are sure, I would advise checking if the first index is a
primary key (PrimaryKey property should be true).
Alternately, you can use the Keys collection instead of the Indexes
collection, and check that the Type property of the Key is adKeyPrimary.

Thérčse


0 new messages