如何在 ADO.NET 4 中检索主键元数据?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从
information_schema.table_constraints
中检索主键:You can retrieve the primary key from the
information_schema.table_constraints
:有关 GetSchema 和
PrimaryKeys
集合的 MSDN 页面。然而它不能与 SQL Server 一起使用,正如它在顶部所说的一些模式集合是不完整的,例如 SQL Server,它不包含主键的集合。
事实上,它的读取意味着 GetSchema 作为跨数据库模式检索的手段并不可靠。
大多数主要 RDBMS 支持的另一个选项是查询 information_schema 以获得所有主键。
您可以按位置顺序将其连接到列的 KEY_COLUMN_USAGE
请注意,尽管 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 topSome 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.
You can join this to KEY_COLUMN_USAGE for the columns by position order
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.
是否有一个统一的 API 来获取所有后端的所有元数据(包括主键信息) - 答案是“否”
理想情况下,每个后端的 DbConnection.GetSchema 覆盖应该提供各种集合来获取所有可能的元数据,但遗憾的是这不是案件。我通常按顺序使用以下方法
对于 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
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.
有一个库,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.