如何取access 表的主键(100)

  • 主题发起人 主题发起人 www.ynxbs.com
  • 开始时间 开始时间
W

www.ynxbs.com

Unregistered / Unconfirmed
GUEST, unregistred user!
如何取access 表的主键?
 
须引用“Microsoft ADO Ext. 2.7 For DDL and Security” Public Function GetPrimaryKeyName(strTable As String) Dim kyForeign As New ADOX.Key Dim cat As New ADOX.Catalog cat.ActiveConnection = ADOCnn GetPrimaryKeyName = "" For Each kyForeign In cat.Tables(strTable).Keys If kyForeign.Type = adKeyPrimary Then GetPrimaryKeyName = kyForeign.Name Exit For '常量 值 说明 'adKeyPrimary 1 默认值。此键是主键。 'adKeyForeign 2 此键是外键。 'adKeyUnique 3 此键是唯一键。 End If Next End Function 得到的主键名就是 “primarykey1”之类,已在VB6+Accesss2000上测试通过。结贴。
 
vb 对我没有用
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ADODB, DB, DBTables, StdCtrls;type TForm1 = class(TForm) btn1: TButton; con1: TADOConnection; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses ADOInt;{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);const COLUMN_NAME = 'COLUMN_NAME'; TABLE_NAME = 'Test';var Fields: Recordset;begin try if not con1.Connected then begin con1.Open; end; Fields := con1.ConnectionObject.OpenSchema(adSchemaPrimaryKeys, VarArrayOf([Null, Null, TABLE_NAME]), EmptyParam); while not Fields.EOF do begin ShowMessage('PrimaryKey:' + VarToStr(Fields.Fields[COLUMN_NAME].Value)); Fields.MoveNext; end; finally con1.Close; end;end;end.
 
接受答案了.
 
后退
顶部