如何获取一张ACCESS表的主键字段名?我在大富翁上看看一篇文章,但是有点问题。 ( 积分: 50 )

  • 主题发起人 主题发起人 darlingpeng
  • 开始时间 开始时间
D

darlingpeng

Unregistered / Unconfirmed
GUEST, unregistred user!
var<br>&nbsp;&nbsp;tb:&nbsp;TADOTable;<br>&nbsp;&nbsp;iCount:&nbsp;integer;<br>&nbsp;&nbsp;tb:TADOTable;<br>&nbsp;&nbsp;PrimaryKey:&nbsp;String;<br>begin<br>&nbsp;&nbsp;Tb&nbsp;:=&nbsp;TADOTable.create(nil);<br>&nbsp;&nbsp;tb.connection&nbsp;:=&nbsp;//你的connection<br>&nbsp;&nbsp;tb.TableName&nbsp;:=&nbsp;//你的tablename;<br>&nbsp;&nbsp;tb.Active&nbsp;:=&nbsp;False;<br>&nbsp;&nbsp;tb.IndexDefs.Update;<br>&nbsp;&nbsp;for&nbsp;iCount&nbsp;:=&nbsp;0&nbsp;to&nbsp;tb.IndexDefs.Count&nbsp;-&nbsp;1&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;ixprimary&nbsp;in&nbsp;tb.IndexDefs[iCount].Options&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrimaryKey&nbsp;:=&nbsp;PrimaryKey&nbsp;+&nbsp;tb.IndexDefs.Items[0].Fields;<br>end;&nbsp;&nbsp;<br>这是&nbsp;duhai_lee&nbsp;这位兄弟写的,但是我执行到&nbsp;tb.IndexDefs.Update就报错,<br>could&nbsp;not&nbsp;convert&nbsp;varinat&nbsp;of&nbsp;type(null)&nbsp;into&nbsp;type(string)&nbsp;&nbsp;<br>是不是我主键或者索引格式有问题,有哪位哥们知道的,帮帮忙。
 
顶下,<br>别沉了&nbsp;<br>有没有知道的,帮帮小弟
 
有没有人知道啊<br>急啊
 
知道的哥们帮帮小弟了
 
昨天到现在都没人回复啊
 
哎,贴子&nbsp;石沉大海了
 
难道没有人知道吗?<br>或者用其他方法把主键字段名列举出来
 
这是从我的应用中摘出来,稍微修改了一下,可得到所有主键的字段名.<br>FConnection&nbsp;是个&nbsp;TADOConnection<br><br>uses<br>&nbsp;&nbsp;Classes,&nbsp;Variant,&nbsp;ADODB,&nbsp;ADOInt;<br><br>function&nbsp;TSGADODBDriver.GetPrimaryKeyFields(const&nbsp;TableName:&nbsp;string;<br>&nbsp;&nbsp;PrimaryKeys:&nbsp;TStrings):&nbsp;Boolean;<br>var<br>&nbsp;&nbsp;Recordset:&nbsp;_Recordset;<br>begin<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;PrimaryKeys.Clear;<br>&nbsp;&nbsp;&nbsp;&nbsp;Conntect;<br>&nbsp;&nbsp;&nbsp;&nbsp;Recordset&nbsp;:=&nbsp;FConnection.ConnectionObject.OpenSchema(adSchemaPrimaryKeys,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VarArrayOf([Null,&nbsp;Null,&nbsp;TableName]),&nbsp;EmptyParam);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;not&nbsp;Recordset.EOF&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrimaryKeys.Add(Recordset.Fields.Item['COLUMN_NAME'].Value);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Recordset.MoveNext;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;True;<br>&nbsp;&nbsp;except<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;False;<br>&nbsp;&nbsp;end;<br>end;
 
樓上正解,看一下OpenSchema幫助,可以得到很多東西的
 
好的,我试一下,
 
could&nbsp;not&nbsp;convert&nbsp;varinat&nbsp;of&nbsp;type(null)&nbsp;into&nbsp;type(string)&nbsp;<br>抱着个错误肯定是主键或是其他字段信息有问题,也就说主键或者一些段的信息没有设置,才会出现null
 
后退
顶部