如何遍历DatagridView中的行和列以将其值插入mysql表中

时间:2016-06-23 20:58:55

标签: mysql vb.net winforms datagridview

好吧,我有一堆.txt文件,我用vb.net导入DataGridView,这些文件总是有不同的列,就像这两个例子一样:

第一个和第二个DataGridView enter image description here

我正在使用下一个代码循环遍历DataGridView的行和列,并且代码也将数据插入到mysql表中,现在,我制作的流程,它是下一个:

  • 我打开.txt文件。
  • 我尝试使用First DataGridView插入数据,但是由于错误,它不应该插入到表中的数据,然而它会这样做,虽然它只有4个列,但是当我检查我的数据库,它显示一切搞砸了,它复制了列,如下图。
  • 然后我"导出"他们到mysql表。当我这样做时,我会在上图中看到错误。

http://i.stack.imgur.com/7uGMQ.jpg

这是我的代码:

 
Private Sub ExportarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExportarToolStripMenuItem.Click

        Dim conn As MySqlConnection = New MySqlConnection("Server=localhost;user=root;password=1234;database=chafa;port=3306")
        conn.Open()

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



        Dim col1, col2, col3, col4, col5, col6, col7, col8, col9, col10

        Dim tabla As New DataTable


        For i = 2 To DgvDatos.Rows.Add - 1 Step 1
            For j = 0 To Me.DgvDatos.Columns.Count - 1

                col1 = DgvDatos.Rows(i).Cells(j).Value()
                col2 = DgvDatos.Rows(i).Cells(j).Value()
                col3 = DgvDatos.Rows(i).Cells(j).Value()
                col4 = DgvDatos.Rows(i).Cells(j).Value()
                col5 = DgvDatos.Rows(i).Cells(j).Value()
                col6 = DgvDatos.Rows(i).Cells(j).Value()
                col7 = DgvDatos.Rows(i).Cells(j).Value()
                col8 = DgvDatos.Rows(i).Cells(j).Value()
                col9 = DgvDatos.Rows(i).Cells(j).Value()
                col10 = DgvDatos.Rows(i).Cells(j).Value()


                comm.CommandText = "insert into ejemplo(col1,col2,col3,col4,col5,col6,col7,col8,col9,col10) values('" & col1 &_ "','" & col2 & "','" & col3 & "','" & col4 & "','" & col5 & "','" & col6 & "','" & col7 & "','" & col8 & "','" & col9 & "','" & col10 & "')"
                comm.ExecuteNonQuery()
            Next
        Next



        MessageBox.Show("Datos Agregados Correctamente")
        conn.Close()

    End Sub

我做错了什么?提前致谢并对不起如果我问了一个重复的问题。

1 个答案:

答案 0 :(得分:0)

我使用sql而不是mysql做了类似的事情,但也许它可以帮助你:

query = String.Empty
        query &= "UPDATE schedule SET Task = @Task, Complete = @Complete, Start_date = @Start_date, "
        query &= "Due_date = @Due_date, JRID = @JRID, Task_Manager = @Task_Manager, Entered_By = @Entered_By, Time_Entered = @Time_Entered "
        query &= "WHERE TaskID = @TaskID "
        query &= "IF @@ROWCOUNT = 0 INSERT INTO schedule ( TaskID, Task, start_date, Due_Date, Complete, Task_Manager, JRID, Entered_By, Time_Entered)"
        query &= " VALUES ( @TaskID, @Task, @start_date, @Due_Date, @Complete, @Task_Manager, @JRID, @Entered_By, @Time_Entered);"
        If MainSchedule.isokclicked = 1 Then
            For Each row As DataGridViewRow In MainSchedule.DataGridView1.Rows
                If Not (row.Cells(0).Value = Nothing) Then
                    insertcommand.Parameters.Clear()
                    insertcommand.CommandText = query
                    insertcommand.Parameters.AddWithValue("@TaskID", row.Cells(0).Value)
                    insertcommand.Parameters.AddWithValue("@Complete", "False")
                    insertcommand.Parameters.AddWithValue("@Task", row.Cells(1).Value)
                    insertcommand.Parameters.AddWithValue("@Start_date", row.Cells(2).Value)
                    insertcommand.Parameters.AddWithValue("@Due_Date", row.Cells(3).Value)
                    insertcommand.Parameters.AddWithValue("@JRID", txtJRID.Text)
                    insertcommand.Parameters.AddWithValue("@Task_Manager", row.Cells(4).Value)
                    insertcommand.Parameters.AddWithValue("@Entered_By", GetUserName())
                    insertcommand.Parameters.AddWithValue("@Time_Entered", Now)
                    insertcommand.ExecuteNonQuery()
                End If
                keypos = keypos + 1
            Next
            Connexion.Close()
        Else
        End If

它只是一个运行参数化查询的基本FOR循环,它可能不是最有效的方法,但它很简单。此代码还将更新数据表,或者如果当前数据库中没有匹配的键,则插入。如果您对我的代码有任何疑问或建议,请不要犹豫,让我知道。