关于取得表主键的问题,不知谁会?

唯伊Coder 2002-12-03 11:29:08
如果只知道表的名称,想取出表的主键,应该怎么取?谢谢!
...全文
89 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2002-12-03
  • 打赏
  • 举报
回复
在楼上给了你DAO方法。
下面是ADO方法:(这是微软网站上的例子)
Private Function FindPrimaryKey( _
tbl As ADOX.Table) As Variant

' Given a particular table, find the primary
' key name, if it exists.

' Returns the name of the primary key's index, if
' it exists, or Null if there wasn't a primary key.

Dim idx As ADOX.Index

For Each idx In tbl.Indexes
If idx.PrimaryKey Then
FindPrimaryKey = idx.Name
Exit Function
End If
Next idx
FindPrimaryKey = Null
End Function
流星尔 2002-12-03
  • 打赏
  • 举报
回复
在asp或者其他客户端程序中执行sp_columns tablename存储过程,然后会返回一个结果集。把data_type为4的取出来就是你的关键子。你在查询分析器中一试就知道了。
timesong 2002-12-03
  • 打赏
  • 举报
回复
"sql里面的isprimarykey", 有吗,能否具体点?

zhoubojun 2002-12-03
  • 打赏
  • 举报
回复
你可以试一试sql里面的isprimarykey
of123 2002-12-03
  • 打赏
  • 举报
回复
Dim dbsNorthwind As Database
Dim tdfEmployees As TableDef
Dim idxLoop As Index

Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set tdfEmployees = dbsNorthwind!Employees

With tdfEmployees

' Enumerate Indexes collection of Employees
' table.
For Each idxLoop In .Indexes
If idxLoop.Primary Then Debug.Print " Primary Key:";
Debug.Print " " & idxLoop.Name
Next idxLoop
End With

dbsNorthwind.Close

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧