我有这个datagridview,它的行是通过单击按钮手动添加的。我想做的是以某种方式循环遍历每一行并将其插入到 mysql 数据库中
这是我当前的代码:
Public Sub addResBulk()
Dim cmdAddRes As New MySqlCommand
Dim addResQry As String
'field remarks left empty until process complete
With cmdAddRes
.Parameters.AddWithValue("@ctrlno", ctrlNo)
.Parameters.AddWithValue("@resdate", dtp_resDate.Value)
.Parameters.AddWithValue("@timestart", cbo_start.SelectedValue)
.Parameters.AddWithValue("@timeend", cbo_end.SelectedValue)
.Parameters.AddWithValue("@claimdate", claimdate)
.Parameters.AddWithValue("@borrowerid", tb_borrowerID.Text)
.Parameters.AddWithValue("@resloc", tb_location.Text)
End With
For row As Integer = 0 To dgv_bulk.Rows.Count - 1
Try
addResQry = "INSERT INTO tbl_test(ControlNo, bCode, Qty, resDate, timeSTART, timeEND, claimDate, borrowerID, resLocation) VALUES " _
+ "(@ctrlno, @bcode, @qty, @resdate, @timestart, @timeend, @claimdate, @borrowerID, @resloc)"
If conn.State = ConnectionState.Open Then
conn.Close()
conn.Open()
Else
conn.Open()
End If
'dgv_bulk.Item(1, o).Value
With cmdAddRes
.Parameters.AddWithValue("@bcode", dgv_bulk.Item(1, row).Value)
.Parameters.AddWithValue("@qty", dgv_bulk.Item(2, row).Value)
qryRes = .ExecuteNonQuery
End With
conn.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
Next row
End Sub
但是,错误会妨碍将每一行成功插入数据库。它告诉我参数@ctrlno 已经定义。另一个错误告诉我我需要一个有效且开放的连接...
有任何想法吗?
提前致谢!