您的位置:首页 > 产品设计 > UI/UE

演示UniqueConstraint类的使用

2006-12-21 22:08 169 查看
(摘录自《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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: