如何将X控件引用到FOR循环中?

时间:2017-12-04 17:13:07

标签: vb.net winforms ms-access oledb

我有三个名为StandardPath_TextBoxA1StandardPath_TextBoxA2StandardPath_TextBoxA3FOR循环的文本框,我试图保存他们的text值进入ms访问数据库文件。我正在尝试下面这样的事情,但我的语法错了......任何想法?

For i = 1 To 3
Dim str(i) As String
str(i) = "INSERT INTO StandardPaths ([TagNum], [Title], [Path]) values (?,?,?)"

Dim cmd As OleDbCommand = New OleDbCommand(str(i), MyConnection)
cmd.Parameters.Add(New OleDbParameter("TagNum", CType("A" & i, String)))
cmd.Parameters.Add(New OleDbParameter("Title", CType(StandardPath_LabelA(i).Text), String)))
cmd.Parameters.Add(New OleDbParameter("Path", CType(StandardPath_TextBoxA(i).Text), String)))

Try
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    MyConnection.Close()
Catch ex As Exception
    MsgBox(ex.Message)
End Try
Next

编辑:

语法错误位于此处:

cmd.Parameters.Add(New OleDbParameter("Title", CType(StandardPath_LabelA(i).Text), String)))
cmd.Parameters.Add(New OleDbParameter("Path", CType(StandardPath_TextBoxA(i).Text), String)))

1 个答案:

答案 0 :(得分:1)

试试这个。它修复了原始版本中的一些类型不匹配,以及删除不必要的变量和冗余。

Dim sql As String = "INSERT INTO StandardPaths ([TagNum], [Title], [Path]) values (?,?,?)"
'DON'T RE-USE YOUR DATABASE CONNECTION, EXCEPT FOR SHORT BURSTS IN TIGHT LOOPS LIKE THIS
Using connection As New OleDbConnection("connection string here"), _
      cmd As New OleDbCommand(sql, connection)

    'Guessing at column types and lengths. Use actual types from your database here
    cmd.Parameters.Add("TagNum", OleDbType.VarChar, 2) 
    cmd.Parameters.Add("Title", OleDbType.VarWChar, 100)
    cmd.Parameters.Add("Path", OleDbType.VarWChar, 512)

    'Do this just once, outside the loop, to avoid needing to repeatedly re-negotiate with the DB. Let the USING black take care of closing the connection
    connection.Open()

    For i = 1 To 3

        Dim TitleLabel As Control = Me.Controls.Find("StandardPath_LabelA" & i.ToString(), True).First()
        Dim PathBox As Control = Me.Controls.Find("StandardPath_TextBoxA" & i.ToString(), True).First()

        cmd.Parameters(0).Value = "A" & i.ToString()
        cmd.Parameters(1).Value = TitleLabel.Text
        cmd.Parameters(2).Value = PathBox.Text

        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    Next
End Using

如果您已将这些控件分组到相关面板或类似控件中,或者将它们添加到数组中,那就更好了。这可以提供比Controls.Find()更好的选择,但我在问题中没有看到这一点。

另一个选择,因为项目数量很少,就是这样做:

Dim sql As String = _ 
    "INSERT INTO StandardPaths ([TagNum], [Title], [Path]) values (?,?,?);" & VbCrLf & _
    "INSERT INTO StandardPaths ([TagNum], [Title], [Path]) values (?,?,?);" & VbCrLf & _
    "INSERT INTO StandardPaths ([TagNum], [Title], [Path]) values (?,?,?);"

Using connection As New OleDbConnection("connection string here"), _
      cmd As New OleDbCommand(sql, connection)

    'Guessing at column types and lengths. Use actual types from your database here
    cmd.Parameters.Add("TagNum1", OleDbType.VarChar, 2).Value = "A1"
    cmd.Parameters.Add("Title1", OleDbType.VarWChar, 100).Value = StandardPath_LabelA1.Text
    cmd.Parameters.Add("Path1", OleDbType.VarWChar, 512).Value = StandardPath_TextBoxA1.Text
    cmd.Parameters.Add("TagNum2", OleDbType.VarChar, 2).Value = "A2"
    cmd.Parameters.Add("Title2", OleDbType.VarWChar, 100).Value = StandardPath_LabelA2.Text
    cmd.Parameters.Add("Path2", OleDbType.VarWChar, 512).Value = StandardPath_TextBoxA2.Text
    cmd.Parameters.Add("TagNum3", OleDbType.VarChar, 2).Value = "A3" 
    cmd.Parameters.Add("Title3", OleDbType.VarWChar, 100).Value = StandardPath_LabelA3.Text
    cmd.Parameters.Add("Path3", OleDbType.VarWChar, 512).Value = StandardPath_TextBoxA3.Text

    connection.Open()

    Try
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Using

我知道重复的代码通常被认为是坏的,但对于三个项目,有时循环只是矫枉过正。在这种情况下,跳过循环还可以减少到数据库的单个原子行程。