Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Dear all,
I create a class Emp with properties same columns name of a table in database. Then i declare a List as
VB
Dim lst As New List(Of Emp)
.
I run project and insert, update, delete items in lst. I want to save data which has changed into table in database in one time.
Pls help me!

P/S: I am using LINQ to do.
Posted
Updated 31-May-12 2:55am
v2

Then you'll have to write the code to check each record against the one stored in the database, then take appropriate action. You won't be able to use LINQ to do this.

You might want to add some kind of "dirty" flag to your Emp class as well as a field that explains what was done to the object, such as Added, Deleted, Changed. Then you could quickly scan the object list and determine what has to be done for each record, if anything, to sync it up with the database. This would prevent you from having to consult the database for every single record in the List.


Since your Emp class is just a direct reflection of the fields in a database, you could have done the same job with a DataTable instead and just used a DataAdapter object to do all the work for you.
 
Share this answer
 
Comments
Nguyễn Minh Phúc 31-May-12 11:15am    
Dear Dave Kreskowiak,
This is approach which i am thinking. But i do as i must transform Emp class to Datatable after use DataAdapter to do it. But i realy like getting it by using List. It is simply than DataAdapter but i cannot do it now.
Thanks much!
I code here:
VB
For i As Integer = 0 To lst.Count - 1
    Dim t = New Emp() With
        {
            .Code= lst.Item(i).Code,
            .Name = lst.Item(i).Name,
        }
    db.Emp.Attach(t, True)
    db.Refresh(Data.Linq.RefreshMode.KeepCurrentValues, t)
Next
db.SubmitChanges()

It show error: Cannot add an entity with a key that is already in use
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900