微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何将这些数据行更改提交回DB

我之前没有真正使用过数据集.我使用了很多LINQ / Entity Framework.

这是我编写的代码(它是交换机的一部分):

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;

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

> Dr.AcceptChanges
> 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,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)
                }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐