(摘录自《C#函数实用手册》冶金工业出版社)
代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataTable testDT = new DataTable("student");
DataColumn testDC = new DataColumn("Id", Type.GetType("System.Int32"));
testDT.Columns.Add(testDC);
testDC = new DataColumn("Name",Type.GetType("System.String"));
testDT.Columns.Add(testDC);
testDC = new DataColumn("School",Type.GetType("System.String"));
testDT.Columns.Add(testDC);
DataColumn[] testDCA = new DataColumn[2];
testDCA[0] = testDT.Columns["Id"];
testDCA[1] = testDT.Columns["Name"];
UniqueConstraint testUC = new UniqueConstraint("IdNameConstraint",testDCA);
testDT.Constraints.Add(testUC);
foreach(UniqueConstraint uc in testDT.Constraints)
{
// 使用Equals方法判断当前的UniqueConstraint对象是否与指定对象相同
if (testUC.Equals(uc))
{
Console.WriteLine("识别到主键约束:" + testUC.ConstraintName);
Console.WriteLine("该约束的哈希代码:" + testUC.GetHashCode());
}
}
Console.ReadLine();
}
}
}
*****************************************
结果:
识别到主键约束:IdNameConstraint
该约束的哈希代码:37121646