Board index » delphi » Getting Primary Key fields of an ADO dataset...

Getting Primary Key fields of an ADO dataset...

Hi,

how do I get the fields that make up the primary key of an ADO dataset?
My provider is SQL Server 2000.  

Point 1:
--------
  For some reason, iterating through each of
TADODataSet.Fields[x].IsIndexField property always returns false.

Point 2:
--------
After some delving into ADODB and ADOInt, and reading "Programming ADO"
by David Sceppa,  I came upon the Attributes property of fields in
native ADO,

From reading ADODB, I surmised that the Attributes property is filled in
this section of code...line 5892

procedure TADODataSet.CreateDataSet;

  procedure CreateFields;
  var
    Options, I: Integer;
  begin
    for I := 0 to FieldDefs.Count - 1 do
    with FieldDefs[I] do
    begin
      if Required then
        Options := 0 else
        Options := adFldIsNullable + adFldMayBeNull;
      if (DataType in [ftMemo, ftBlob]) and (Size = 0) then
        Size := High(Integer);
      Recordset.Fields.Append(Name, FieldTypeToADOType(DataType), Size,
Options);
    end;
  end;

begin
  CheckInactive;
  InitFieldDefsFromFields;
  FRecordsetObject := CreateADOObject(CLASS_Recordset) as _Recordset;

Sceppa's text notes that the Attribute property, adFldKeyColumn, will
indicate if a field is part of the primary key.  However, according to
the code above, the CreateFields function doesn't even attempt to make
this property part of the Options property passed into the creation of
the Fields property.  I'm assuming from this code that Delphi does not
make use of this property either (adFldKeyColumn).  All Delphi seems to
care about the native ADO field properties is whether they are required
or not...

How can I then get the fields that make up the primary key?  Is there a
generic SQL statement to do that?

Thanks a bunch...
JB

 

Re:Getting Primary Key fields of an ADO dataset...


Quote
>How can I then get the fields that make up the primary key?  Is there a
>generic SQL statement to do that?

Try
exec sp_pkeys  'YourTableName'

The examine the dataset returned

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Getting Primary Key fields of an ADO dataset...


Thanks for the response...I've figured out how it's done.  

However, your suggestion returns a blank dataset with columns
(TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, KEY_SEQ,
PK_NAME)

The table I tested it with has 6 fields that make up the PK.  

I'm just curious now, but what was I supposed to see?

Thanx
JB

Quote
> exec sp_pkeys  'YourTableName'

> The examine the dataset returned

Re:Getting Primary Key fields of an ADO dataset...


Quote
>However, your suggestion returns a blank dataset with columns
>(TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, COLUMN_NAME, KEY_SEQ,
>PK_NAME)

>The table I tested it with has 6 fields that make up the PK.  

>I'm just curious now, but what was I supposed to see?

With that stored procedure I get a dataset returned with those fields and one
row for each field in the Primary key.   I am using SQL server 7
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads