加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:16:49 所属栏目:百科 来源:网络整理
导读:我之前没有真正使用过数据集.我使用了很多LINQ / Entity Framework. 这是我编写的代码(它是交换机的一部分): if (!DataHelper.DataSourceIsEmpty(dsUp)) { //get datarow collection from dataset DataRowCollection drc = dsUp.Tables[0].Rows; //Loop thr
我之前没有真正使用过数据集.我使用了很多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)
                }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读