0

我有这个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 已经定义。另一个错误告诉我我需要一个有效且开放的连接...

有任何想法吗?

提前致谢!

4

2 回答 2

1

这也可以

For x As Integer = 0 To yourdatagridviewname.Rows.Count - 1

Dim str1 = "INSERT INTO [expensesTB]([amount],[description],[expensesDate])values(@amount,@description,@expensesDate)"
            Dim com As New OleDb.OleDbCommand(str1, con)
            com.Parameters.AddWithValue("@amount", yourdatagridviewname.Rows(x).Cells(2).Value)
            com.Parameters.AddWithValue("@description", yourdatagridviewname.Rows(x).Cells(1).Value)
            com.Parameters.AddWithValue("@expensesDate", thisyear.ToString("yyyy/MM/dd"))
            com.ExecuteNonQuery()
            com.Dispose()
        Next
于 2015-10-04T09:19:47.380 回答
0

如果要将在 Datagridview 中手动输入的数据插入 Mysql 数据库,请尝试此操作。

希望这会帮助你。


Public Sub addResBulk()

        ''create connection 
        Dim conn As MySqlConnection = New MySqlConnection(connectionString)
        conn.Open()

        Dim comm As MySqlCommand = New MySqlCommand()
        comm.Connection = conn

        ''insert data to sql database row by row
        Dim ctrlno, bcode, resdate, timestart, timeend, claimdate, borrowerID, resloc As Object
        Dim qty As Double
        Dim tbl_test As New DataTable


        For i = 0 To DataGridView1.Rows.Add - 1 Step 1
            ctrlno = DataGridView1.Rows(i).Cells(0).Value()
            bcode = DataGridView1.Rows(i).Cells(1).Value()
            qty = DataGridView1.Rows(i).Cells(2).Value()
            resdate = DataGridView1.Rows(i).Cells(3).Value()
            timestart = DataGridView1.Rows(i).Cells(4).Value()
            timeend= DataGridView1.Rows(i).Cells(5).Value()
            claimdate = DataGridView1.Rows(i).Cells(6).Value()
            borrowerID = DataGridView1.Rows(i).Cells(7).Value()
    resloc = DataGridView1.Rows(i).Cells(8).Value()

            comm.CommandText = "insert into tbl_test(ControlNo,bCode,Qty,resDate,timeSTART,timeEND,claimDate,borrowerID,resLocation ) values('" & ctrlno & "','" & bcode & "','" & qty & "','" & resdate & "','" & timestart & "','" & timeend & "','" & claimdate & "','" & borrowerID & "','" & resloc & "')"
            comm.ExecuteNonQuery()
        Next
        conn.Close()
        Me.Close()
    End If

End Sub
于 2013-09-18T02:14:06.817 回答