我正在尝试从两个DataTables向创建的相关视图中添加新行。初始相关视图的创建类似于以下代码,
DataTable employeeCollection = new DataTable();
DataTable employeeCollection1 = new DataTable();
DataSet ds = new DataSet();
DataTable dataTable;
DataView relatedView;
public Form1()
{
dataTable = this.GetDataTable();
relatedView = dataTable.DefaultView[0]["Employee"] as DataView;
}
public DataTable GetDataTable()
{
employeeCollection.Columns.Add("EmployeeID",typeof(int));
employeeCollection.Columns.Add("EmployeeName",typeof(string));
employeeCollection.Columns["EmployeeName"].ColumnName = "Employee Name";
employeeCollection.Columns.Add("CustomerID",typeof(string));
employeeCollection.Columns["CustomerID"].ColumnName = "Customer ID";
employeeCollection.Columns.Add("Country",typeof(string));
employeeCollection.Columns.Add("Date",typeof(DateTime));
employeeCollection.Rows.Add(1001,"Belgim","Yhgtr","US",new DateTime(2019,10,15));
employeeCollection.Rows.Add(1002,"Oliver","Johanesberg","UK",new DateTime(2018,11,29));
employeeCollection.Rows.Add(1004,"James","Chicago",new DateTime(2016,5,13));
employeeCollection.Rows.Add(1005,"Beverton","bergs","Spain",new DateTime(2015,6,29));
employeeCollection.Rows.Add(1009,"Dintin","Britain",new DateTime(2014,3,10));
employeeCollection.Rows.Add(1010,"Joysie","Oregon","China",new DateTime(2012,11));
employeeCollection.Rows.Add(1017,"George","SwitzerLand",new DateTime(2017,7,17));
employeeCollection1.Columns.Add("EmployeeID",typeof(int));
employeeCollection1.Columns.Add("Country",typeof(string));
employeeCollection1.Columns.Add("Date",typeof(DateTime));
employeeCollection1.Rows.Add(1001,15));
employeeCollection1.Rows.Add(1002,29));
employeeCollection1.Rows.Add(1004,13));
employeeCollection1.Rows.Add(1005,29));
employeeCollection1.Rows.Add(1009,10));
employeeCollection1.Rows.Add(1010,11));
employeeCollection1.Rows.Add(1017,17));
ds.Tables.Add(employeeCollection);
ds.Tables.Add(employeeCollection1);
ds.Relations.Add(new DataRelation("Employee",ds.Tables[0].Columns["Country"],ds.Tables[1].Columns["Country"]));
return ds.Tables[0];
}
在我的相关视图中,外键列Country
的值是US
,因此添加国家(如美国)的新行会将新行正确地添加到relatedView中,如下所示:
private void button1_Click(object sender,EventArgs e)
{
System.Data.DataRow dataRow = relatedView.Table.NewRow();
dataRow["EmployeeID"] = 101;
dataRow["Country"] = "US";
relatedView.Table.Rows.Add(dataRow);
// New row added to related view properly.
}
当我添加具有不同外键值的新行时,它将引发标准异常
ForeignKeyConstraint员工要求子键值()必须存在于父表中
此代码:
private void button2_Click(object sender,EventArgs e)
{
System.Data.DataRow dataRow = relatedView.Table.NewRow();
dataRow["EmployeeID"] = 101;
dataRow["Country"] = "UK";
relatedView.Table.Rows.Add(dataRow);
// Exception for invalid case thrown.
}
但是,当我们添加带有空外键的新行时,不会引发任何异常,也不会将新行添加到相关视图中。代码只是无例外地执行,但是我相信应该抛出异常,因为外键值为空。
private void button2_Click(object sender,EventArgs e)
{
System.Data.DataRow dataRow = relatedView.Table.NewRow();
dataRow["EmployeeID"] = 101;
dataRow["Country"] = null;
relatedView.Table.Rows.Add(dataRow);
// New row is not added to related view and also exception is not thrown.
}
我的问题是:为什么仅在外键值不同时才引发ForeignKeyConstraint
异常,为什么在value为null或空时不引发该异常?
有人可以澄清吗?