乍一看,我意识到这个问题已经被问过很多次了——我想我已经阅读/搜索了很多——我已经非常努力地检查了我发现的每一个建议——
也许我只是需要另一双眼睛?
问题是这样的:我有一个主表和两个详细表。它们在我的数据源中的定义与在数据库中的定义完全相同,具有相同的关系和键/FK 字段/类型。
我可以手动将数据输入表中,并在查询时得到预期的结果(左连接等)。此外,我可以在应用程序中正确查看记录 - 加入正确的父记录。
但是,这些表/BindingSources 中只有一个工作正常。我已经看过我知道的所有地方,但看不出有什么不同?
BargeDetails 表格绑定源完美运行。ShiftDeadTime 不会将记录保存/插入到数据库中。
我检查了 bindingsource.count = 1,并且底层 bindingsource.current 具有预期值。
SQL 跟踪显示没有尝试插入/更新。
我正在使用 Visual Studio 2010、ADO.NET、.NET 4 和 WinForms 以及 SQL Server 2008。数据源是经过设计的,而不是使用设计器先编写代码。我将详细绑定源更改为指向正确的数据源/数据成员=FK ...
在我的 _Load() 中 - 我的 ta.fill() 的顺序看似正确;
private void frmShiftReport_Load(object sender, EventArgs e)
{
this.shiftsTableAdapter.Fill(this.dsShiftReport.Shifts);
this.shiftDeadTimeTableAdapter.Fill(this.dsShiftReport.ShiftDeadTime);
this.bargeDetailTableAdapter.Fill(this.dsShiftReport.BargeDetail);
this.vw_BargeLookupTableAdapter.Fill(this.dsShiftReport.vw_BargeLookup);
this.vw_CommodityLookupTableAdapter.Fill(this.dsShiftReport.vw_CommodityLookup);
}
绑定源似乎设置相同;
shiftDeadTimeBindingSource
datasource = shiftsBindingSource1
datamember = FK_ShiftDeadTime_Shifts
bargeDetailBindingSource
datasource = shiftsBindingSource1
datamember = FK_BargeDetail_Shifts
dgShiftDeadTime
datasource = shiftDeadTimeBindingSource
在我的点击/保存事件中,看起来我有正确/相似的 EndEdit()s 和 UpdateAll();
// barge details
private void shiftsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.shiftsBindingSource1.EndEdit();
this.bargeDetailBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.dsShiftReport);
}
// shiftDeadTime
private void toolStripButton8_Click(object sender, EventArgs e)
{
this.Validate();
this.shiftsBindingSource1.EndEdit();
this.shiftDeadTimeBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.dsShiftReport);
}
最后,在 datasource.designer.cs - 两个关系/约束似乎设置相同;
fkc = new global::System.Data.ForeignKeyConstraint("FK_BargeDetail_Shifts", new global::System.Data.DataColumn[] {
this.tableShifts.IDColumn}, new global::System.Data.DataColumn[] {
this.tableBargeDetail.ShiftIDColumn});
this.tableBargeDetail.Constraints.Add(fkc);
fkc.AcceptRejectRule = global::System.Data.AcceptRejectRule.None;
fkc.DeleteRule = global::System.Data.Rule.Cascade;
fkc.UpdateRule = global::System.Data.Rule.Cascade;
fkc = new global::System.Data.ForeignKeyConstraint("FK_ShiftDeadTime_Shifts", new global::System.Data.DataColumn[] {
this.tableShifts.IDColumn}, new global::System.Data.DataColumn[] {
this.tableShiftDeadTime.ShiftIDColumn});
this.tableShiftDeadTime.Constraints.Add(fkc);
fkc.AcceptRejectRule = global::System.Data.AcceptRejectRule.None;
fkc.DeleteRule = global::System.Data.Rule.Cascade;
fkc.UpdateRule = global::System.Data.Rule.Cascade;
this.relationFK_BargeDetail_Shifts = new global::System.Data.DataRelation("FK_BargeDetail_Shifts", new global::System.Data.DataColumn[] {
this.tableShifts.IDColumn}, new global::System.Data.DataColumn[] {
this.tableBargeDetail.ShiftIDColumn}, false);
this.Relations.Add(this.relationFK_BargeDetail_Shifts);
this.relationFK_ShiftDeadTime_Shifts = new global::System.Data.DataRelation("FK_ShiftDeadTime_Shifts", new global::System.Data.DataColumn[] {
this.tableShifts.IDColumn}, new global::System.Data.DataColumn[] {
this.tableShiftDeadTime.ShiftIDColumn}, false);
this.Relations.Add(this.relationFK_ShiftDeadTime_Shifts);
这真让我抓狂!