2维for循环出错

时间:2019-04-15 17:59:48

标签: vb.net

我正在尝试制作一个二维的for循环,以存储DataGridView到SQL数据库中的每一行和每一列,并且行的顺序相同。但是,当我将其保存在数据库中时,它会一遍又一遍地重复。

比方说,我有5列和3行,我想要的是按相同的顺序存储3行,但它存储了15个重复的序列。

这是我的代码。我不知道我在做什么错。

For i As Integer = 0 To DataGridView1.RowCount - 1
    For j As Integer = 0 To DataGridView1.ColumnCount - 1
        cmd.CommandText = "Insert into SInvoice  Values('SIV-" & (TextBox4.Text) + 1 & "',0," & j + 1 & ",(select nodeno from productmast where arabic_name= '" & DataGridView1.Item(1, i).Value & "')," & DataGridView1.Item(2, i).Value & ",0.000000,'2019-04-04 13:40:00'," & DataGridView1.Item(4, i).Value & "," & DataGridView1.Item(3, i).Value & ",'',0,1,1,1,1,'04/04/2019 13:39:44','',1.000000,1,0,'','b',0.000000,5,0.000000," & DataGridView1.Item(3, i).Value & ",'Nos',1.000000,'','','','','','','','','','','','','','','','','','','','','',0,3,'" & Label4.Text & "',1,1,1,1,1,1,0.000000)"
        'DataGridView1.Item(i, 2).Value
        'DataGridView1.Item()

        cmd.ExecuteNonQuery()
    Next
Next

1 个答案:

答案 0 :(得分:0)

据我所知,希望能对您有所帮助。

For MyRows As Integer = 0 To 3
        For MyCols As Integer = 0 To 5
            cmd.CommandText = "Insert into SInvoice  Values('" & DataGridView1.Rows(MyRows).Cells(MyCols).Value & "','" & DataGridView1.Rows(MyRows).Cells(MyCols + 1).Value & "','" & DataGridView1.Rows(MyRows).Cells(MyCols + 2).Value & "','" & DataGridView1.Rows(MyRows).Cells(MyCols + 3).Value & "','" & DataGridView1.Rows(MyRows).Cells(MyCols + 4).Value & "')"
            cmd.ExecuteNonQuery()
        Next
    Next

最佳问候     ^ _ ^