您的位置:首页 > 大数据 > 人工智能

演示ForeignKeyConstraint类的使用

2006-12-21 22:06 148 查看
(摘录自《C#函数实用手册》冶金工业出版社)

代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataSet testDS = new DataSet("Test");
DataTable testDT = new DataTable("student");
DataTable testDTA = new DataTable("course");

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("CID",Type.GetType("System.Int32"));
testDT.Columns.Add(testDC);

testDC = new DataColumn("CName",Type.GetType("System.String"));
testDTA.Columns.Add(testDC);
testDC = new DataColumn("CID",Type.GetType("System.Int32"));
testDTA.Columns.Add(testDC);

testDS.Tables.Add(testDTA);
testDS.Tables.Add(testDT);

DataColumn pDC = testDS.Tables["student"].Columns["ID"];
DataColumn cDC = testDS.Tables["course"].Columns["CID"];
ForeignKeyConstraint testFK = new ForeignKeyConstraint("IdConstraint",pDC,cDC);
testFK.DeleteRule = Rule.SetNull; // 当值被删除时将值设为空
testFK.UpdateRule = Rule.Cascade; // 当更新某一行时,删除或更新相关的行
testFK.AcceptRejectRule = AcceptRejectRule.Cascade; // 在关联中级联更改
testDS.Tables["course"].Constraints.Add(testFK);
testDS.EnforceConstraints = true;
foreach(ForeignKeyConstraint fkc in testDS.Tables["course"].Constraints)
{
// 使用Equals方法判断当前的ForeignKeyConstraint对象是否与指定对象相同
if(testFK.Equals(fkc))
{
Console.WriteLine("识别到外键约束: " + testFK.ConstraintName);
Console.WriteLine("该约束的哈希代码:" + testFK.GetHashCode());
}
}
Console.ReadLine();
}
}
}

*************************

执行结果:

识别到外键约束:IdConstraint
该约束的哈希代码:36610825
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: