I want to insert 1500 rows to Ms access database from vb.net datagridview.
Inserting up to 400 rows no issue, but above 400 rows its showing error - System resource exceeded.
Im using below code. The error is highlighting to:
readinputs = dbup.ExecuteReader() and sometimes
.ExecuteNonQuery()
Dim Dbcon As New OleDbConnection(connStr)
Dbcon.Open()
Dim query As String
Dim dbup As New OleDbCommand
Dim readinputs As OleDbDataReader
For x As Integer = 0 To IncomingMailDGV.Rows.Count - 1
Dim received As String = IncomingMailDGV.Rows(x).Cells(0).Value
Dim subject As String = IncomingMailDGV.Rows(x).Cells(1).Value
Dim contents As String = IncomingMailDGV.Rows(x).Cells(2).Value
query = "SELECT ReceivedDateTime, Subject, MessageContents FROM IncomingAlerts WHERE ReceivedDateTime = @ReceivedDateTime AND MessageContents =@MessageContents"
dbup = New OleDbCommand(query, Dbcon)
dbup.Parameters.AddWithValue("ReceivedDateTime", received)
dbup.Parameters.AddWithValue("MessageContents", contents)
readinputs = dbup.ExecuteReader()
If readinputs.HasRows = False Then
Dim InsertData As String
InsertData = "INSERT INTO IncomingAlerts(ReceivedDateTime, Subject, MessageContents) Values (@ReceivedDateTime, @Subject, @MessageContents)"
dbup = New OleDbCommand(InsertData)
dbup.Parameters.AddWithValue("ReceivedDateTime", received)
dbup.Parameters.AddWithValue("Subject", subject)
dbup.Parameters.AddWithValue("MessageContents", contents)
With dbup
.CommandText = InsertData
.Connection = Dbcon
.ExecuteNonQuery()
End With
End If
Next
Because of the loop, you are creating up to 2
OleDbCommand
objects per row (one for theSELECT
and maybe one for theUPDATE
), but never disposing of them. You could usecmd.Parameters.Clear
to reuse them, but I would chop that thing up into a control procedure to make it simpler. Something like this:Then helpers which are self contained and clean up after themselves:
I also made them a little shorter by using the constructor overloads. For instance, with OleDbCommand, I pass the SQL and the connection to it when it is created rather than set those properties individually.
As is, it gets done only once. There are other things you could do such as just use SQL
Count
to determine if the are any matching rows etc. Using a DataTable and FindRow would also prevent having to hot the DB to see if something exists.The main point is disposing of
Connection
,Command
andDataReader
objects when you are done with them.