DataTable.PrimaryKey 填充了唯一聚集索引,而不是主键
DataTable.PrimaryKey is populated with unique clustered index, not primary key
我有一个 SQL 服务器 table MyTable
,其中为列 A
设置了主键,还为列 B
设置了唯一聚集索引,并且C
。
这次通话后:
(new SqlDataAdapter(new SqlCommand($"select top 0 * from MyTable", conn))).FillSchema(theDataTable, SchemaType.Mapped);
theDataTable
的 .PrimaryKey
设置为列 B
和 C
,而不是 A
。
为什么会这样?
正如我所说,这实际上包含在文档中 FillSchema(DataTable, SchemaType, IDbCommand, CommandBehavior):
FillSchema also configures the PrimaryKey and Constraints properties
according to the following rules:
If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the
DataTable.
If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the
unique columns are nonnullable. If any of the columns are nullable, a
UniqueConstraint is added to the ConstraintCollection, but the
PrimaryKey property is not set.
If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the
DataTable.
Note that primary keys and unique constraints are added to the
ConstraintCollection according to the preceding rules, but other
constraint types are not added.
If a unique clustered index is defined on a column or columns in a SQL
Server table and the primary key constraint is defined on a separate
set of columns, then the names of the columns in the clustered index
will be returned. To return the name or names of the primary key
columns, use a query hint with the SELECT statement that specifies the
name of the primary key index. For more information about specifying
query hints, see Query Hint (Transact-SQL).
如果您确实需要从 .PrimaryKey
获取主键详细信息,那么您需要按照文档的建议使用提示。如果没有您 table 的 DDL(包括索引),我们将无法告诉您提示是什么。
我有一个 SQL 服务器 table MyTable
,其中为列 A
设置了主键,还为列 B
设置了唯一聚集索引,并且C
。
这次通话后:
(new SqlDataAdapter(new SqlCommand($"select top 0 * from MyTable", conn))).FillSchema(theDataTable, SchemaType.Mapped);
theDataTable
的 .PrimaryKey
设置为列 B
和 C
,而不是 A
。
为什么会这样?
正如我所说,这实际上包含在文档中 FillSchema(DataTable, SchemaType, IDbCommand, CommandBehavior):
FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:
If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.
If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.
If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.
Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added.
If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. For more information about specifying query hints, see Query Hint (Transact-SQL).
如果您确实需要从 .PrimaryKey
获取主键详细信息,那么您需要按照文档的建议使用提示。如果没有您 table 的 DDL(包括索引),我们将无法告诉您提示是什么。