📄 sample13.cs
字号:
namespace apiBook
{
using System;
using System.Data;
using System.IO;
public class TestClass
{
public static void Main()
{
TestClass test=new TestClass();
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)
{
if(testFK.Equals(fkc))
//使用Equals方法判断当前的ForeignKeyConstraint对象是否与指定对象相同
{
Console.WriteLine("识别到外键约束:"+testFK.ConstraintName);
Console.WriteLine("该约束的哈希代码:"+testFK.GetHashCode());
//使用GetHashCode方法获取
//ForeignKeyConstraint对象的此实例的哈希代码
}
}
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -