如何在组框内循环文本框,然后将其仅插入1列的sql数据库中?

时间:2019-03-26 01:48:14

标签: vb.net

你好,我想制作一个程序,它在groupbox中有很多文本框,并将其插入ms sql数据库中。我的问题是我无法弄清楚如何循环文本框,例如:Dim as i Integer = 1 .. textbox(i).text ...因此,如果使用“ For i”循环它,它将控制从textbox1到textbox10的文本框。

dim cmd as new sqlcommand("insert daily (day)values(@day)",conn) 

For i = 0 To 3

Dim lab(i) As Label
i = i + 1
cmd.Parameters.AddWithValue("@day", lab(i).Text)
GroupBox1.Controls.Add(lab(i))
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
cmd.Parameters.Clear()

Next

1 个答案:

答案 0 :(得分:0)

让我们先检查一下您发布的代码。

    'Good, declaring and initializing a Command, sending query and connection to constructor.
    Dim cmd As New SqlCommand("insert daily (day)values(@day)", conn)
    'OK, a For loop
    For i = 0 To 3
        'Declare a vaiable as an array with 1 element of type Label
        Dim lab(i) As Label
        'Increment your For loop counter???
        'This will make your loop skip numbers and iterations
        i = i + 1
        'Since your array is empty and only has one element available at index 0
        'This line will not work.
        'On the very first iteration i will now equal 1 which is already out of bounds for your array
        'Assuming you actually created a Label in this loop, how would it have anything
        'in the Text property before it was even visible
        cmd.Parameters.AddWithValue("@day", lab(i).Text)
        'Can't add a non existant control, you never created an instance of a Label (would need the New keyword)
        GroupBox1.Controls.Add(lab(i))
        'Good, opening the connection at the last minute.
        conn.Open()
        cmd.ExecuteNonQuery()
        'Good, closing as soon as possible, but what if there was an error executing your command
        'your connection would not be closed!
        conn.Close()
        cmd.Parameters.Clear()
    Next

现在,基于@Ahmed Abdelhameed和@jmcilhinney的评论,我们将创建一个DataTable,并使用For Each循环填充它。然后使用DataAdapter.Update将数据发送到数据库。

在这种情况下,我们仅向.InsertCommand提供了DataAdapter,因为DataTable中的所有数据都是新行。在大多数情况下,我们还会提供UpdateCommandDeleteCommand。可以通过提供SelectCommand并使用CommandBuilder为您生成这些信息。

注意:您必须使用.Parameters.Add方法的重载,该方法包含数据库列名。这提供了DataAdapter的映射。请仔细检查数据库中的数据类型,字段大小和列名,因为我使用了猜测。

在循环中使用DataAdapter而不是.ExecuteNonQuery的原因是为了减少到数据库的往返次数。由于您说过“很多文本框”,因此应该可以提高性能。

Using...End使用块可确保即使发生错误也可以关闭和处置数据库对象。

Private Sub BuildAndFillDataTable()
    Dim dt As New DataTable
    dt.Columns.Add("Day")
    For Each tb As TextBox In GroupBox1.Controls.OfType(Of TextBox)
        Dim dr As DataRow = dt.NewRow
        dr("Day") = tb.Text
        dt.Rows.Add(dr)
    Next
    BatchUpdate(dt)
End Sub

Private Sub BatchUpdate(dt As DataTable)
    Using cn As New SqlConnection("Your connection String")
        Using da As New SqlDataAdapter()
            Using cmd As New SqlCommand("Insert Into daily (day) values(@day)", cn)
                cmd.Parameters.Add("@day", SqlDbType.VarChar, 50, "day")
                da.InsertCommand = cmd
                da.Update(dt)
            End Using
        End Using
    End Using
End Sub