Here I am again, asking for some help.
I have a code that is working fine for inserting a record in my table
Dim insert4 = "insert into f_logs(f_id, log_Date,log_action,log_Destination,log_status,log_account,log_remarks) " _
& " values(@id,@date,@destination,@destination2,@status,@account,@remarks)"
cmd3 = New MySqlCommand(insert4, conn)
/* cadd is my function for adding values to the parameters */
/* The function simplifies the adding of parameter*/
/* x.Parameters.AddWithValue(para, val) */
cadd(cmd3, "@id", docu_id)
cadd(cmd3, "@date", ToMySql(dt1.Value))
cadd(cmd3, "@destination", get_destination())
cadd(cmd3, "@destination2", tbDestination.Text)
cadd(cmd3, "@status", get_status())
cadd(cmd3, "@account", tbAccount.Text)
cadd(cmd3, "@remarks", tbRemarks.Text)
cmd3.ExecuteNonQuery()
/* cpara is a function for clearing the parameters*/
/* The function simplifies the clearing of parameter */
/* since i am using it frequently*/
cpara(cmd3)
Here comes the problem. I need to create the logs for each record in my table. I fetch the needed id's for the logs using this code:
SELECT f_id FROM gsis_new.filelocation t2 " _
& " where t2.f_location =@loc"
And then i put the result in a hidden datagridview.
I could use a loop to insert all the logs generated for each id, but executing each query one by one will take took to long to finish specially when I am inserting like 100 logs.
/* Like using this logic in adding logs */
Start
Add value to parameters
Execute Query
clear parameters
Repeat
That is why i want to insert all the logs in a single query, but i do not know how to do that. I needed to add parameters in my query during run-time.
/* I want to use this logic in adding logs */
Add parameters that is equal to the number of logs to be inserted.
Start
Add value to the parameters
Repeat
Execute Query
clear parameters
----------
UPDATE:
I tried to insert it one by using this code:
Dim count As Integer = (dgMatchID.Rows.Count() - 1)
Try
Dim watch As New Stopwatch()
If count > 0 Then
cadd(cmd3, "@id", "0")
cadd(cmd3, "@date", ToMySql(dt1.Value))
cadd(cmd3, "@destination", get_destination())
cadd(cmd3, "@destination2", tbDestination.Text)
cadd(cmd3, "@status", get_status())
cadd(cmd3, "@account", tbAccount.Text)
cadd(cmd3, "@remarks", tbRemarks.Text)
For i As Integer = 0 To count - 1
cmd3.Parameters("@id").Value = dgMatchID.Rows(i).Cells("f_id").Value.ToString
cmd3.ExecuteNonQuery()
watch.Start()
Next
watch.Stop()
Dim seconds As Double = watch.Elapsed.TotalSeconds()
MsgBox(seconds)
Else
MsgBox("Nothing to insert!!!")
End If
And it takes 8.0429649 seconds to complete the insertion of 215 rows. Is there any way to shorten the execution time, I am thinking that if I only execute the MySQLCommand once, the time it need will be shorten.
If you are using .NET 4+ you can insert using a Parallel Foreach statement. This will execute in parallel which will insert several records at the same time. This should be faster. I am unable to test this but functionally it should work.
Dim Options As ParallelOptions = New ParallelOptions With {.MaxDegreeOfParallelism = 100}
The Options above is not needed you could just remove this all together. Removing will let .NET decide how many threads to run based on resources. You would need to make sure your MaxPool is high enough to take advantage of the multiple connections that will be opened
Parallel.ForEach(dgMatchID.Rows.Cast(Of DataGridViewRow), Options,
Function()
Dim insert4 As String = "insert into f_logs(f_id, log_Date,log_action,log_Destination,log_status,log_account,log_remarks) " _
& " values(@id,@date,@destination,@destination2,@status,@account,@remarks)"
Dim conn As New MySqlConnection()
Dim cmd3 As MySqlCommand = New MySqlCommand(insert4, conn)
cmd3.Parameters.AddWithValue("@id", "")
cmd3.Parameters.AddWithValue("@date", "")
cmd3.Parameters.AddWithValue("@destination", "")
cmd3.Parameters.AddWithValue("@destination2", "")
cmd3.Parameters.AddWithValue("@status", "")
cmd3.Parameters.AddWithValue("@account", "")
cmd3.Parameters.AddWithValue("@remarks", "")
Return New With {
Key .Conn = conn,
Key .Cmd = cmd3
}
End Function,
Function(dr, state, localInit)
Dim row As DataGridViewRow = dr
localInit.Cmd.Parameters("@id").Value = row.Cells("f_id").Value.ToString
localInit.Cmd.ExecuteNonQuery()
Return localInit
End Function,
Function(localInit)
localInit.Cmd.Dispose()
localInit.Conn.Dispose()
Return localInit
End Function)
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.