开发者

Update local datatable from database

I have been working on this problem for a few days and was hoping someone might have an answer. I guess the more general description of the problem is that multiple users will access a database from multiple instances of an appli开发者_StackOverflow中文版cation and I would like to keep the local copy of the data as current as possible. This is my current attempt at doing so, but I am open to any way of keeping the local copy in sync with the database.

I have two data tables:

dtOriginal is loaded at the start of the application and the user is allowed to edit it.

dtFromDb is loaded when the user changes the displayed record on the form and it only contains the current values of the record now being displayed after the change.

I am attempting to update the dtOriginal table with those values from dtFromDb only if the row in dtOriginal is not marked as "modified".

I have been attempting this with the merge method of the System.Data.DataTable class.

StronglyTypedDataTable dtOriginal = StronglyTypedDataTableAdapter.GetData();
// User modifies one record through collectionview...
// displayed record is then changed and the following occurs
StronglyTypedDataTable dtFromDb = StronglyTypedDataTableAdapter.GetDataByID(currentID);
dtOriginal.Merge(dtFromDb, true);

I would have expected that any rows which were marked as "unmodified" would have been updated with any new local values from the database if they had changed in another instance of the program and were propagated back to the database, but this is not the case. It looks like dtFromDb does get the updated values from the DB, but I must not be using the merge method correctly as the updated values are not propagated to the local copy.

Any ideas would be greatly appreciated.

Thanks,

Tyler


I see that you ate trying to solve manually something that does not belong to your application: data concurrency!

When you want to save changed just send to the database and if your design and data layer is ok either save will fail if somebody changed something or it will go just fine.

Or you can imagine to add a refresh button to your page or grid and if user clicks refresh normally local changes are lost. being distributed application let it be distributed and who saves first will win. i dont think gou should load twice and synch those two tables client side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜