如何将这些数据行更改提交回数据库

本文关键字:提交 数据库 数据 | 更新日期: 2023-09-27 17:56:49

我以前没有真正使用过数据集。我使用了很多LINQ/实体框架。

这是我编写的代码(它是开关的一部分):

  if (!DataHelper.DataSourceIsEmpty(dsUp))
                {
                    //get datarow collection from dataset
                    DataRowCollection drc = dsUp.Tables[0].Rows;
                    //Loop through dataset
                    foreach (DataRow dr in drc)
                    {
                        //get current dataset row sortid
                        int sortID = Convert.ToInt32(dr["SortID"]);
                        { 
                        //if its the row above then minus one
                        if (sortID == nodeAbove)
                        {
                            int newID = Convert.ToInt32(dr["SortID"].ToString());
                            newID--;
                            dr["SortID"] = newID;
                            //TODO: save changes back to original ds

                        }
                        }
                    }
                }
                break;

我一直在尝试这样的事情:

  • 博士。接受更改
  • dsUp.AcceptChanges(dr)
  • drc.copyto(dsUP)
  • dsUp.Merge(drc)

以及许多其他类似的尝试都没有奏效。在谷歌搜索这个主题时,我发现的所有结果都使用表格适配器......当我在CMS中工作时,我得到的数据如下:

DataSet dsUp = tree.SelectNodes(CurrentSite, path, cultureCode, true, classnames, where, orderby);

任何关于将更改保存回数据库的帮助将不胜感激干杯。

自发布以来,我也尝试了这种方法,不幸的是没有奏效:

        //dataset to hold results before merge 
        DataSet DSResults = tree.SelectNodes(CMSContext.CurrentSite.SiteName, path, cultureCode, true, classnames);
        DSResults.Clear();

        if (!DataHelper.DataSourceIsEmpty(dsUp))
        {
            //get datarow collection from dataset
            DataRowCollection drc = dsUp.Tables[0].Rows;
            //Loop through dataset
            foreach (DataRow dr in drc)
            {
                //get current dataset row sortid
                int sortID = Convert.ToInt32(dr["SortID"]);
                { 
                //if its the row above then minus one
                if (sortID == nodeAbove)
                {
                    int newID = Convert.ToInt32(dr["SortID"].ToString());
                    newID--;
                    dr["SortID"] = newID;
                    dr.AcceptChanges();
                    DSResults.Tables[0].Rows.Add(dr);

                }
                }
            }
        }
        //save changes back to original ds
        dsUp.Merge(DSResults);
        dsUp.AcceptChanges();
        break;

如何将这些数据行更改提交回数据库

场景背后的数据集实现了 UnitOfWork 模式,用于跟踪自从数据库中提取数据以来所做的所有更改。

您在这里错过的是调用数据集更新以将所有更改保存回数据库

我已将更新添加到您的代码中:

if (!DataHelper.DataSourceIsEmpty(dsUp))
                {
                    //get datarow collection from dataset
                    DataRowCollection drc = dsUp.Tables[0].Rows;
                    //Loop through dataset
                    foreach (DataRow dr in drc)
                    {
                        //get current dataset row sortid
                        int sortID = Convert.ToInt32(dr["SortID"]);
                        { 
                        //if its the row above then minus one
                        if (sortID == nodeAbove)
                        {
                            int newID = Convert.ToInt32(dr["SortID"].ToString());
                            newID--;
                            dr["SortID"] = newID;
                            //TODO: save changes back to original ds
                        }
                        }
                    }
                  //you can save here as the dataset will keep track of all the changes
                  YourDataAdapter.Update("tableName",dsUp)
                }