TableAdapter不更新标识

时间:2013-12-11 14:26:29

标签: c# sql-server strongly-typed-dataset tableadapter

我使用TableAdapter更新数据库,如下所示:

var changes = anpDatabase.AnpCollection.GetChanges();
anpCollectionTableAdapter.Update((AnpDatabase.AnpCollectionDataTable)changes);
anpDatabase.AnpCollection.Merge(changes);
anpDatabase.AnpCollection.AcceptChanges();

但是这还不足以更新标识主列...我尝试将适配器的Insert Command更改为以下内容:

INSERT INTO AnpCollection
                  (ParentCollectionId, StartDate, EndDate, Completed, IsPartial, Partitioned, IsAutomaticallyGenerated)
VALUES     (@ParentCollectionId,@StartDate,@EndDate,@Completed,@IsPartial,@Partitioned,@IsAutomaticallyGenerated);
SELECT CollectionId, ParentCollectionId, StartDate, EndDate, Completed, IsPartial, Partitioned, IsAutomaticallyGenerated FROM AnpCollection WHERE (CollectionId = SCOPE_IDENTITY())

但是,如果我这样做,而不是更新列,将出现一个具有正确标识值的新行...这不是我想要的...有人可以帮助我吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

INSERT末尾留下SELECT语句,似乎正确的代码是:

anpCollectionTableAdapter.Update(anpDatabase.AnpCollection);

而不是:

var changes = anpDatabase.AnpCollection.GetChanges();
anpCollectionTableAdapter.Update((AnpDatabase.AnpCollectionDataTable)changes);
anpDatabase.AnpCollection.Merge(changes);
anpDatabase.AnpCollection.AcceptChanges();