hi friends
I am new to
vb.net , I am having difficulty to insert two rows in database table from datagridview. I can insert one row , I would like to know how do i run nonqueryCommand.Parameters inside For Each loop twice to insert two row. please help me
thanks
Code:
Dim thisConnection As New OleDbConnection("Provider=Microsoft.ACE.⦠Source=C:\Dtb.accdb")
'Create Command object
Dim nonqueryCommand As OleDbCommand = thisConnection.CreateCommand()
Try
' Open Connection
thisConnection.Open()
Console.WriteLine("Connection Opened")
' Create INSERT statement with named parameters
nonqueryCommand.CommandText = _
"INSERT INTO myTable (Col1, Col2) VALUES (@Col1, @Col2)"
' Add Parameters to Command Parameters collection
nonqueryCommand.Parameters.Add("@Col1"⦠OleDbType.VarChar, 50)
nonqueryCommand.Parameters.Add("@Col2"⦠OleDbType.VarChar, 50)
' Prepare command for repeated execution
nonqueryCommand.Prepare()
' Data to be inserted
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
nonqueryCommand.Parameters("@Col1").Va⦠= row.Cells(0).Value.ToString
nonqueryCommand.Parameters("@Col2").Va⦠= row.Cells(1).Value.ToString
End If
Next
nonqueryCommand.ExecuteNonQuery()
Catch ex As OleDbException
' Display error
Console.WriteLine("Error: " & ex.ToString())
Finally
' Close Connection
thisConnection.Close()
Console.WriteLine("Connection Closed")
End Try