SqlServer: 下面语句中的b.name列就会返回你的表的主键 select b.name from sysobjects a,syscolumns b left outer join (select g.id,g.colid from sysindexes f, sysindexkeys g where f.id = g.id and f.indid = g.indid and f.indid > 0 and f.indid < 255 and (f.status & 2048)<>0) h on b.id = h.id and b.colid = h.colid where a.id = b.id and a.id = object_id('你的表名') and h.id is not null
oracle中试试用这个语句,如果有多个主键,要循环取出 select column_name from user_constraints uc,user_cons_columns ucc where constraint_type = 'P' and uc.table_name = 表名 and uc.constraint_name = ucc.constraint_name 表名自己指定,其它照写。然后fieldbyname('column_name')
Dataset.fields.Fields[Index].IsIndexField=true
begin
//这里就是主键了
end
但是前提就是要保证你的控件的active是true的
oracle没有用过的 不知道有没有这个存储过程的
select b.name
from sysobjects a,syscolumns b
left outer join (select g.id,g.colid from sysindexes f,
sysindexkeys g where f.id = g.id and f.indid = g.indid and f.indid > 0
and f.indid < 255 and (f.status & 2048)<>0) h on b.id = h.id and b.colid = h.colid
where a.id = b.id
and a.id = object_id('你的表名')
and h.id is not null
如果时oracle可以从user_objects里面查到,如果时sql server也可以从它的系统标结构里面查到,但是关键是每一中数据库存放主键的位置不一样,不知道delphi有没有办法可以得到,据说ado的metadata工具好像可以得到,不知道是否准确
select column_name from user_constraints uc,user_cons_columns ucc
where constraint_type = 'P' and uc.table_name = 表名 and uc.constraint_name = ucc.constraint_name
表名自己指定,其它照写。然后fieldbyname('column_name')