循环遍历DGV中的列数据并插入到SQL中

时间:2015-01-21 22:11:55

标签: sql vb.net loops datagridview

我目前正在使用vb.net express 2013在Windows窗体应用程序中工作。我有一个datagridview,我试图遍历一个列并将该列中的所有日期插入到sql数据库中。出了点问题。这是我的代码:

     For cn As Integer = 0 To Datagridview.RowCount - 1
        Dim variable1 As Date = Datagridview.Rows(cn).Cells(1).Value

        'sql code
        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("INSERT INTO table1 (columns) VALUES (values)", conn1)
                With comm1.Parameters
                    .AddWithValue("@Value1", variable1)
                    .AddWithValue("@Name", CBName.ValueMember)
                End With

comm1.ExecuteReader()                     结束使用                     conn1.Close()                 结束使用         下一步

变量1是日期,我只是从组合框中提取名称。值和列是一组用于设置表的值。我真的没有我的代码说“列”和“值”。我没有收到错误消息。它只是没有写入sql表。

最终错过了我的执行命令......我给家人带来了耻辱

1 个答案:

答案 0 :(得分:1)

最终代码有效,错过了我的执行命令,没有什么可看的,继续前进。

        Try

            Using conn1 As New SqlConnection(connstring)
                conn1.Open()
                Using comm1 As New SqlCommand("INSERT INTO table1 (columns) VALUES (values)", conn1)
                    With comm1.Parameters
                        .AddWithValue("@Shear", variable1)
                        .AddWithValue("@Name", combobox)
                    End With
                    comm1.ExecuteReader()
                End Using
                conn1.Close()
            End Using

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    Next