将子记录保存到数据库时出现问

时间:2012-04-17 19:54:30

标签: c# ado.net bindingsource master-detail

乍一看,我意识到这个问题已被多次询问 - 而且我认为我已阅读/搜索了其中许多问题 - 而且我已经非常努力地检查了我发现的每一个建议 -

也许我只需要另一双眼睛?

问题是: 我有一个主表和两个详细表。它们在我的数据源中与在数据库中完全相同,具有相同的关系和键/ FK字段/类型。

我可以手动将数据输入到表中,并在查询时得到预期的结果(左连接等)。此外,我可以在应用程序中正确查看记录 - 加入正确的父记录。

但是,这些表/ BindingSources中只有一个正常工作。 我到处看都知道要看,看不出有什么区别?

BargeDetails表绑定源工作正常。 ShiftDeadTime不会将记录保存/插入数据库。

我已经检查过bindingsource.count = 1,底层的bindingsource.current有预期的值。

SQL跟踪显示没有尝试插入/更新。

我正在使用Visual Studio 2010,ADO.NET,.NET 4和WinForms以及SQL Server 2008。 设计了数据源,而不是首先使用设计器编写代码。 我将detail bindingsources更改为指向正确的datasource / datamember = 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()和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);

这让我抓狂!

1 个答案:

答案 0 :(得分:0)

在这种情况下我建议做什么:检查您的更改是否出现在DataSet中 通过调用

this.dsShiftReport.GetChanges();

这样您就可以知道更改是否至少反映在DataSet中。

我假设您在表单上有某种DataGridView控件并将表绑定到DataGridView。我可能错了(我有一段时间没有使用WinForms和DataSets)但是如果你点击“保存”按钮之前点击“输入”和/或尝试移动到DataGridView中的另一行怎么办 - 只是想知道你的在这种情况下,数据将被保存。