I have not really worked with datasets before. I use a lot of LINQ / Entity Framework.
Here is the code I have written (it is one part of a switch):
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 sor开发者_如何学运维tid
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;
I have been trying things like:
- dr.AcceptChanges
- dsUp.AcceptChanges(dr)
- drc.copyto(dsUP)
- dsUp.Merge(drc)
and many other similar attempts that have not worked. When googling for this topic all the results I have found use a table adapter... as im working in a cms I get my data like so:
DataSet dsUp = tree.SelectNodes(CurrentSite, path, cultureCode, true, classnames, where, orderby);
Any assistance on getting the changes saved back to the db would be massively appreciated Cheers.
Since posting I have also tried this method which unfortunately did not work:
//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;
The dataset behind the scene implements the UnitOfWork pattern keeping track of all the changes you have made since you have pulled out the data from the database.
what you missin here is calling the dataset update to save all the changes back on the DB
I have added the update to your code:
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)
}
精彩评论