Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I do not want to use the Loop
It's a slow process


VB
Dim StrNumber, StrBarcode, StrProductName, StrQty, StrUnitPrice, StrUnit, StrTotal As String
        Dim insertcommenttext As String = ""
        Dim Cm As New MySqlCommand
        Try
            For i As Integer = 0 To Me.DataGridList.Rows.Count - 1
                AUTO_NUMBER()

                StrNumber = Me.DataGridList.Rows(i).Cells(0).Value.ToString() 
                StrBarcode = Me.DataGridList.Rows(i).Cells(1).Value.ToString()
                StrProductName = Me.DataGridList.Rows(i).Cells(2).Value.ToString() 
                StrQty = Me.DataGridList.Rows(i).Cells(3).Value.ToString() 
                StrUnitPrice = Me.DataGridList.Rows(i).Cells(4).Value.ToString()
                StrUnit = Me.DataGridList.Rows(i).Cells(5).Value.ToString() 
                StrTotal = Me.DataGridList.Rows(i).Cells(6).Value.ToString() 

                insertcommenttext = "INSERT INTO Temporarybill (AutoNumber,TemID,Number,Barcode,ProductName,Qty,UnitPrice,Unit,Total,CustomerID) VALUES('" _
                    & IntAutoNumber & "','" & "TEM-" & IntTem_ID & "','" & StrNumber & "','" & StrBarcode & "','" & StrProductName & _
                    "','" & StrQty & "','" & StrUnitPrice & "','" & StrUnit & "','" & StrTotal & "','" & TbxBoxCustomersID.Text & "')"

                With Cm
                    .CommandType = CommandType.Text
                    .CommandText = insertcommenttext
                    .Connection = StrSettingConn
                    .ExecuteNonQuery()
                End With
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
Posted
Updated 22-Mar-15 12:18pm
v3
Comments
Maciej Los 22-Mar-15 17:27pm    
Does DataGridView has datasource?

DO NOT use concatenation to form SQL statments. EVER! Use a parameterized statement.

This will help you immensely in this case:

0) Before the loop:
0.1) Create the INSERT command, with parameter markers.
0.2) Add the parameters.
1) Inside the loop:
1.1) Set the parameter Values.
1.2) Execute the command.

Possibly use a transaction as well.
 
Share this answer
 
It's not the loop itself that causes your method to be slow. It's the fact that you execute a database-INSERT for each entry in your datagrid individually.

1) Move the database-Insert-Stuff out of your loop (With Cm .... End With)

2) In your loop, concatenate all the Insert-Statements into one Batch-Statement, preferably using a StringBuilder *

3) When your loop is done, your database-Insert executes that one "big" Insert-Statement in one go.

Edit: There are also some other improvements you can make, first and foremost to use Sql-Parameters. Please refer to one of my previous answers:
How to insert datetime in sql server 2008 using VB 2010?[^]

* Edit: I'm talking here about concatenating the "full" Insert-Statements into a so-called Batch-Statement. Not about concatenating a single Insert-Statement like you currently do, from multiple string-literals and variables. That should be done instead with Sql-Parameters which allows to write the Insert-Statement either as one single string-literal when not using a Batch-Statement, or with a formatted string-literal for building a Batch-Statement with different Sql-Parameter-Names for each Insert-Statement.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900