gpt4 book ai didi

c# - DataTable 上的 UNIQUE 约束

转载 作者:行者123 更新时间:2023-12-04 02:15:51 25 4
gpt4 key购买 nike

我可以在 DataTable 上有一个集群 key 吗?在 C# 中?

我的代码中要求对 3 列的可能组合具有唯一性进行约束.....

最佳答案

您真正需要的是一个唯一约束 在您的 DataTable .群集键是 SQL Server 磁盘功能,不适用于 DataTable .

退房 MSDN doc on DataTable constraints :

The UniqueConstraint object, which can be assigned either to a single column or to an array of columns in a DataTable, ensures that all data in the specified column or columns is unique per row. You can create a unique constraint for a column or array of columns by using the UniqueConstraint constructor.



所以尝试这样的事情:
// this is your DataTable
DataTable custTable ;

// create a UniqueConstraint instance and set its columns that should make up
// that uniqueness constraint - in your case, that would be a set of *three*
// columns, obviously! Adapt to your needs!
UniqueConstraint custUnique =
new UniqueConstraint(new DataColumn[] { custTable.Columns["CustomerID"],
custTable.Columns["CompanyName"] });

// add unique constraint to the list of constraints for your DataTable
custTable.Constraints.Add(custUnique);

这应该对你有用!

关于c# - DataTable 上的 UNIQUE 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11843696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com