C# 如何将这些数据行更改提交回数据库
我以前没有真正使用过数据集。我使用了很多LINQ/实体框架 以下是我编写的代码(它是交换机的一部分): 我一直在尝试这样的事情:C# 如何将这些数据行更改提交回数据库,c#,asp.net,dataset,datarow,C#,Asp.net,Dataset,Datarow,我以前没有真正使用过数据集。我使用了很多LINQ/实体框架 以下是我编写的代码(它是交换机的一部分): 我一直在尝试这样的事情: 博士接受变化 接受更改(dr) drc.copyto(dsUP) dsUp.Merge(drc) 还有许多其他类似的尝试都没有成功。当用谷歌搜索这个主题时,我找到的所有结果都使用表适配器。。。当我在cms工作时,我得到的数据如下: DataSet dsUp = tree.SelectNodes(CurrentSite, path, cultureCode, tru
- 博士接受变化
- 接受更改(dr)
- drc.copyto(dsUP)
- dsUp.Merge(drc)
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)
}
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)
}