views:

198

answers:

1

If I have say for example 100,000 rows to insert/update/delete, and this number will grow constantly. Which of the following is the best approach, or does it not make any difference.

The PeopleRepository AddPeople implementation

public void AddPeople(IEnumerable i)
{
    _Database.people.InsertAllOnSubmit(i);
}

The PeopleRepository AddPerson implementation

public void AddPerson(Person p)
{
    _Database.people.InsertOnSubmit(p);
}

The PeopleRepository Save implementation

public void Save()
{
    _Database.SubmitChanges();
}

InsertAllOnSubmit Implementation

PeopleRepository repo = new PeopleRepository();

List<Person> everyone = new List<Person>();

foreach (var p in myObject.GetPeople())
{
    Person person = new Person
    {
        person.Name = p.Name
    };

    everyone.Add(person);
}

repo.AddPeople(everyone);
repo.Save();

InsertOnSubmit Implementation

PeopleRepository repo = new PeopleRepository();

foreach (var p in myObject.GetPeople())
{
    Person person = new Person
    {
        person.Name = p.Name
    };

    repo.AddPerson(person);
    repo.Save();
}
+1  A: 

If you have such large amounts of changes, you might consider expressing the change in a single SQL UPDATE or INSERT statement.

Steven
+1 because if you do this with LINQ to SQL it will execute an individual query for every insert you do, which no matter how you write your code is going to take longer than some plain SQL statements to execute.
jarrett