I just want to make sure that this is the right code for working with SQLite for fastest performance..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cons As New SQLite.SQLiteConnection
cons.ConnectionString = "Data Source=C:\database.s3db; Version=3"
cons.Open()
Using tx = cons.BeginTransaction()
Dim cmd As SQLite.SQLiteCommand
cmd = cons.CreateCommand()
For i As Integer = 0 To 1000
Try
cmd.CommandText = "INSERT INTO table1 VALUES('" & i & "')"
cmd.ExecuteNonQuery()
Catch ex As Exception
End Try
Next
tx.Commit()
cmd.Dispose()
End Using
cons.Close()
End Sub
As I mentioned, proper way of doing this using parameterized queries, not swallowing the exception, and using statements wherever necessary would look something like this. Do note this is not the fastest way of doing .
Private Sub InsertRows()
Using conn As New SqlConnection
conn.Open()
Using tx = conn.BeginTransaction()
For i As Integer = 0 To 1000
Using cmd = conn.CreateCommand() 'Proper using statements wherever necessary
Try
cmd.CommandText = "INSERT INTO table1 VALUES(@ColumnName)" 'Paramertized queries
cmd.Parameters.AddWithValue("@ColumnName", i)
cmd.ExecuteNonQuery()
Catch ex As Exception
logger.ErrorException(ex) 'Logging the exception or shoe messagebox
End Try
End Using
Next
End Using
End Using
End Sub
Ideally you shouldn't continue execution if there is an exception in a tight loop doing same task. If one fails there is a chance that everything can fail. So in that case you should remove the try/catch inside the loop and wrap it over the for loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.