如何在组框上使用for循环进行更新?

时间:2019-05-03 05:04:14

标签: sql-server vb.net

使用sql-server保存数据后,我即将进行更新。 我在insert方法中使用了相同的代码。

这是我的代码

For i = 1 To Me.Controls.OfType(Of GroupBox).Count
             cmd.Parameters.AddWithValue("@dlog", dtp_date_make.Value)
Dim gb As GroupBox = CType(Me.Controls("Groupbox" & i), GroupBox)
For Each ctrl As Control In gb.Controls
                   If TypeOf ctrl Is Label Then
                       Dim lvl As Label = ctrl
                       cmd.Parameters.AddWithValue("@ptbi", lvl.Text)
                   End If
                   If TypeOf ctrl Is CheckBox Then
                       Dim chck As CheckBox = ctrl
                       cmd.Parameters.AddWithValue("@io", chck.Checked)
                   End If

                   If TypeOf ctrl Is ComboBox Then
                       Dim com As ComboBox = ctrl
                       cmd.Parameters.AddWithValue("@rem", com.Text)
                   End If
                   If TypeOf ctrl Is NumericUpDown Then
                       Dim nud As NumericUpDown = ctrl
                       cmd.Parameters.AddWithValue("@wono", nud.Value)
                   End If
               Next
               conn.Open()
               cmd.ExecuteNonQuery()
               cmd.Parameters.Clear()
               conn.Close()

我的分组框从1到23。更新后 当我查看数据库时,ID为23的Groupbox23从ID 1到ID 22逃避了最后一个ID。

1 个答案:

答案 0 :(得分:0)

也许这样写出来

    For Each GrpBox As GroupBox In Controls.OfType(Of GroupBox)
        For Each Ctrl As Control In GrpBox.Controls
            Select Case Ctrl.GetType
                Case GetType(Label)
                    'do stuff with label'
                    cmd.Parameters.Add("@ptbi", SqlDbType.VarChar).Value = DirectCast(Ctrl, Label).Text

                Case GetType(CheckBox)
                    'do stuff with CheckBox'
                    cmd.Parameters.Add("@io", SqlDbType.Bit).Value = DirectCast(Ctrl, CheckBox).Checked
            End Select
        Next
    Next