后台工作者失败

问题描述 投票:0回答:1

我需要来自后台工作人员的 DoWork 帮助。我收到消息 System.InvalidOperationException:“未为对象实例定义对象引用”,但仅在某些情况下,例如,当我简单地更新数据时,它可以正常工作,现在,当我包含通过 OnRowUpdated 的新记录时,它发生错误,不是在 OnRowUpdated 中,而是在稍后,接近结束时,有时在 Commit 中,有时在 Cnn.Close 中。

当在“PlanOrc”表中仅插入一行时,它工作正常,但如果有与第二个“Services”表相关的行,则会出现错误。没有调用 UI。

现在最奇怪的事情是,当我一步步运行它时,它完美地更新了数据库,没有任何错误!

注意:在 DoWork 之外,整个例程运行完美。

我的部分代码:

        private void BWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            using OleDbConnection cnn = new($"Provider=Microsoft.{provedorJet};Data Source={file};Persist Security Info=False;Mode=Share Exclusive");
            cnn.Open();

            using OleDbTransaction trans = cnn.BeginTransaction(IsolationLevel.ReadUncommitted);

            try
            {
                DataTable dataChanges = DsPro.Tables["Planorc"].GetChanges();
                if (dataChanges != null)
                {
                    AdaPla.UpdateCommand.Connection = cnn; AdaPla.UpdateCommand.Transaction = trans;
                    AdaPla.InsertCommand.Connection = cnn; AdaPla.InsertCommand.Transaction = trans;
                    AdaPla.DeleteCommand.Connection = cnn; AdaPla.DeleteCommand.Transaction = trans;

                    AdaPla.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);

                    qT = AdaPla.Update(DsPro.Tables["PlanOrc"].Select("", "", DataViewRowState.Deleted));
                    DsPro.Relations["PlaSer"].ChildKeyConstraint.AcceptRejectRule = AcceptRejectRule.None;
                    qT = AdaPla.Update(DsPro.Tables["PlanOrc"].Select("", "", DataViewRowState.ModifiedCurrent));
                    if (DsPro.Tables["PlanOrc"].Select("", "", DataViewRowState.Added).Any())
                    {
                        qT = AdaPla.Update(DsPro.Tables["PlanOrc"].Select("", "", DataViewRowState.Added));
                    }
                    DsPro.Relations["PlaSer"].ChildKeyConstraint.AcceptRejectRule = AcceptRejectRule.Cascade;

                    AdaPla.RowUpdated -= new OleDbRowUpdatedEventHandler(OnRowUpdated);
                }

                DataTable dataChangesSer = DsPro.Tables["Servicos"].GetChanges();
                if (dataChangesSer != null)
                {
                    AdaSer.UpdateCommand.Connection = cnn; AdaSer.UpdateCommand.Transaction = trans;
                    AdaSer.InsertCommand.Connection = cnn; AdaSer.InsertCommand.Transaction = trans;
                    AdaSer.DeleteCommand.Connection = cnn; AdaSer.DeleteCommand.Transaction = trans;

                    AdaSer.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated);
                    qT = AdaSer.Update(DsPro.Tables["Servicos"].Select("", "", DataViewRowState.Deleted));
                    DsPro.Relations["SerCom"].ChildKeyConstraint.AcceptRejectRule = AcceptRejectRule.None;
                    qT = AdaSer.Update(DsPro.Tables["Servicos"].Select("", "", DataViewRowState.ModifiedCurrent));
                    if (DsPro.Tables["Servicos"].Select("", "", DataViewRowState.Added).Any())
                    {
                        qT = AdaSer.Update(DsPro.Tables["Servicos"].Select("", "", DataViewRowState.Added));
                    }
                    DsPro.Relations["SerCom"].ChildKeyConstraint.AcceptRejectRule = AcceptRejectRule.Cascade;
                    AdaSer.RowUpdated -= new OleDbRowUpdatedEventHandler(OnRowUpdated);
                }

                ... more things here
                ... mais things here

                trans.Commit();
                cnn.Close();
            }
        }

        protected void OnRowUpdated(object sender, OleDbRowUpdatedEventArgs args)
        {
            ... somethings
            ... no error occurs here

        }

更新 MS Access 数据库。

transactions adapter backgroundworker updating
1个回答
0
投票

我明白了,这就是问题所在,当我添加这个时,一切正常:

dgv1.CellFormatting -= new DataGridViewCellFormattingEventHandler(Table1_CellFormatting);

DoWork()

dgv1.CellFormatting += new DataGridViewCellFormattingEventHandler(Table1_CellFormatting);

© www.soinside.com 2019 - 2024. All rights reserved.