如何在 ADO.NET 4 中检索主键元数据?

发布于 2024-10-13 05:38:47 字数 410 浏览 5 评论 0原文

我正在尝试使用 ADO.NET 4 检索数据库中所有表的主键元数据。GetSchema

() 方法可让您获取大量元数据,包括索引和外键,但没有任何内容可识别主键。对我来说,这似乎是一个明显的遗漏,我想一定是我遗漏了一些东西!

从我所发现的情况来看,似乎有两个“解决方法”(至少对于 SQL Server 而言):

  • 使用 SqlDataReader ExecuteReader() KeyInfo 选项;或
  • 使用 SqlDataAdapter.Fill( dataTable ) 方法,然后在返回的 dataTable 上使用 KeyInfo 属性。

谁能告诉我是否有一种干净的标准方法来识别数据库中的所有主键并检索可在所有数据提供程序实现(包括 Oracle、DB2、MySql)中工作的元数据?

I'm trying to retrieve the Primary Key metadata for all tables in a database using ADO.NET 4.

The GetSchema() methods let you pull down a lot of metadata, including Indexes and Foreign Keys, but there's nothing there to identify Primary Keys. It seems like such a glaring omission to me that I figure it must be me who's missing something!

From what I've been able to find, there seems to be two 'workarounds' (for SQL Server at least):

  • use SqlDataReader ExecuteReader() KeyInfo option; or
  • use SqlDataAdapter.Fill( dataTable ) method and then use KeyInfo property on the returned dataTable.

Can anyone please tell me if there's a clean standard way to identify all Primary Keys in a database and retrieve their metadata that will work across all Data Provider implementations (incl Oracle, DB2, MySql)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青瓷清茶倾城歌 2024-10-20 05:38:47

您可以从 information_schema.table_constraints 中检索主键:

select c.constraint_name, c.table_name, k.column_name
from   information_schema.table_constraints as c
join   information_schema.key_column_usage as k
on     c.constraint_catalog = k.constraint_catalog 
       and c.constraint_schema = k.constraint_schema 
       and c.constraint_name = k.constraint_name 
where  c.constraint_type = 'primary key'
       and c.table_name = 'YourTable'

You can retrieve the primary key from the information_schema.table_constraints:

select c.constraint_name, c.table_name, k.column_name
from   information_schema.table_constraints as c
join   information_schema.key_column_usage as k
on     c.constraint_catalog = k.constraint_catalog 
       and c.constraint_schema = k.constraint_schema 
       and c.constraint_name = k.constraint_name 
where  c.constraint_type = 'primary key'
       and c.table_name = 'YourTable'
不顾 2024-10-20 05:38:47

有关 GetSchema 和 PrimaryKeys 集合的 MSDN 页面。然而它不能与 SQL Server 一起使用,正如它在顶部所说的

一些模式集合是不完整的,例如 SQL Server,它不包含主键的集合。

事实上,它的读取意味着 GetSchema 作为跨数据库模式检索的手段并不可靠。

大多数主要 RDBMS 支持的另一个选项是查询 information_schema 以获得所有主键。

select *
from information_schema.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'PRIMARY KEY'

您可以按位置顺序将其连接到列的 KEY_COLUMN_USAGE

select PK.TABLE_SCHEMA, PK.TABLE_NAME, C.ORDINAL_POSITION, C.COLUMN_NAME
from information_schema.TABLE_CONSTRAINTS PK
inner join information_schema.KEY_COLUMN_USAGE C
    on C.TABLE_SCHEMA = PK.TABLE_SCHEMA
    and C.TABLE_CATALOG = PK.TABLE_CATALOG
    and C.TABLE_NAME = PK.TABLE_NAME
where PK.CONSTRAINT_TYPE = 'PRIMARY KEY'

请注意,尽管 information_schema 具有良好的支持,但它可能在一些小方面有所不同。在 SQL Server 中,table_catalog(=数据库名称)始终仅限于当前数据库,而在 MySQL 中,它将返回任何目录(数据库)中的表,因此您可能需要目录过滤器才能准确。

MSDN Page on GetSchema and the collection PrimaryKeys. However it does not work with SQL Server, as it says at the top

Some schema collections are incomplete, such as SQL Server, which does not contain a collection for primary keys.

In fact, the reading of it implies that GetSchema is not reliable as a means of cross-db schema retrieval.

The other option which is supported by most major RDBMS is to query information_schema for all the primary keys.

select *
from information_schema.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'PRIMARY KEY'

You can join this to KEY_COLUMN_USAGE for the columns by position order

select PK.TABLE_SCHEMA, PK.TABLE_NAME, C.ORDINAL_POSITION, C.COLUMN_NAME
from information_schema.TABLE_CONSTRAINTS PK
inner join information_schema.KEY_COLUMN_USAGE C
    on C.TABLE_SCHEMA = PK.TABLE_SCHEMA
    and C.TABLE_CATALOG = PK.TABLE_CATALOG
    and C.TABLE_NAME = PK.TABLE_NAME
where PK.CONSTRAINT_TYPE = 'PRIMARY KEY'

Take note that even though information_schema has good support, it can be different in small ways. In SQL Server, table_catalog (=db name) is always limited to the current db, whereas in MySQL, it will return tables from any catalog (db), so you may need a catalog filter to be accurate.

ゞ记忆︶ㄣ 2024-10-20 05:38:47

是否有一个统一的 API 来获取所有后端的所有元数据(包括主键信息) - 答案是“否”

理想情况下,每个后端的 DbConnection.GetSchema 覆盖应该提供各种集合来获取所有可能的元数据,但遗憾的是这不是案件。我通常按​​顺序使用以下方法

  1. GetSchema 方法和适当的架构集合
  2. 系统存储过程(如果可用)。 (sys.sp_primarykeys 等)
  3. 查询系统视图(INFORMATION_SCHEMA.KEY_COLUMN_USAGE 等)
  4. 如果所有信息在单个视图中不可用,则使用自定义查询查询系统视图

对于 Sql Server 2005 及更高版本,我使用了系统存储过程 - sys.sp_primary_keys_rowset2

如果需要,我还将提供其他后端的信息。

Is there a unified API to get all the metadata (including Primary Key information) for all backends - answer is 'No'

Ideally the DbConnection.GetSchema overrides for each backend should provide various collections to get all possible metadata, but sadly that is not the case. I usually use the following approaches in that order

  1. GetSchema method with appropriate schema collection
  2. System stored procedures,if available. (sys.sp_primarykeys etc)
  3. Query system views (INFORMATION_SCHEMA.KEY_COLUMN_USAGE etc)
  4. Query the system view(s) with a custom query if all information isn't available in a single view

For Sql Server 2005 and above I've used the system stored procedure - sys.sp_primary_keys_rowset2

If required I'll provide the information for other backends also.

贪了杯 2024-10-20 05:38:47

有一个库,Kailua - ADO.NET API 中被遗忘的方法。 ,这确实为排名前 5 的供应商提供了此元数据和其他元数据。此信息是供应商特定的。

There's a library, Kailua - The forgotten methods in the ADO.NET API. , that does provide this and additional metadata for the top 5 vendors. This info is vendor specific.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文