how is more fast?

Posts   
 
    
eirizarry
User
Posts: 48
Joined: 30-Mar-2011
# Posted on: 04-Apr-2011 16:38:46   

i need to insert 5000 record on the database. I'm doing that with the followind code

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click Dim testinsert As New TestTableEntity

    Dim C1 As Integer
    Dim numrec As Integer

    For C1 = 1 To 5000
        testinsert.R1 = C1
        testinsert.IsNew = True
        testinsert.Save()

        TextBox4.Text = C1
    Next

    MsgBox("FIN")
End Sub

each time a create a new record i save it to the database. is there a way to create all of them in memory first and them save them to the database in just one shot?

thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 04-Apr-2011 17:04:43   

You can add entities to an EntityCollection, and save the collection at the end. Or use a UnitOfWork, both will be similar in this case.

eirizarry
User
Posts: 48
Joined: 30-Mar-2011
# Posted on: 04-Apr-2011 17:08:22   

did you have an example? i can't find one

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Apr-2011 02:38:32   

eirizarry wrote:

did you have an example? i can't find one

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim testinsert As New TestTableEntity
        Dim testCollection As TestTableCollection()

        Dim C1 As Integer
        For C1 = 1 To 5000
            testinsert = new TestTableEntity()
            testinsert.R1 = C1
            testCollection.Add(testinsert)
        Next

        testCollection.SaveMulti()

        '...
    End Sub
David Elizondo | LLBLGen Support Team
eirizarry
User
Posts: 48
Joined: 30-Mar-2011
# Posted on: 05-Apr-2011 03:37:56   

thanks. works nice!!