Jet Database (ms access) ExecuteNonQuery - Can I make it faster?

Posted by bluebill on Stack Overflow See other posts from Stack Overflow or by bluebill
Published on 2010-05-25T19:58:59Z Indexed on 2010/05/25 20:01 UTC
Read the original article Hit count: 219

Filed under:
|
|

Hi all, I have this generic routine that I wrote that takes a list of sql strings and executes them against the database. Is there any way I can make this work faster? Typically it'll see maybe 200 inserts or deletes or updates at a time. Sometimes there is a mixture of updates, inserts and deletes. Would it be a good idea to separate the queries by type (i.e. group inserts together, then updates and then deletes)?

I am running this against an ms access database and using vb.net 2005.

Public Function ExecuteNonQuery(ByVal sql As List(Of String), ByVal dbConnection as String) As Integer
If sql Is Nothing OrElse sql.Count = 0 Then Return 0

Dim recordCount As Integer = 0

Using connection As New OleDb.OleDbConnection(dbConnection)
    connection.Open()
    Dim transaction As OleDb.OleDbTransaction = connection.BeginTransaction()

    'Using cmd As New OleDb.OleDbCommand()
    Using cmd As OleDb.OleDbCommand = connection.CreateCommand
        cmd.Connection = connection
        cmd.Transaction = transaction

        For Each s As String In sql
            If Not String.IsNullOrEmpty(s) Then
                cmd.CommandText = s
                recordCount += cmd.ExecuteNonQuery()
            End If
        Next

        transaction.Commit()
    End Using
End Using

Return recordCount

End Function

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about ms-access