获取字符串数组的每个其他值

时间:2014-03-07 17:08:24

标签: arrays vb.net

我正在尝试使用数组的每个其他元素填充组合框

数组包含这样的数据

(0) server
(0) sqlInstance
(1) server
(1) sqlInstance
(2) server
(2) sqlInstance

我希望将所有sqlInstance值添加到组合框中。除了可能是另一个阵列之外,不知道怎么做。

由于

Private Sub GetSQLServers()

        Dim oSQLApp As New SQLDMO.Application
        Dim oServerNames As SQLDMO.NameList
        Dim strServer As String
        Dim ServerArr() As String

        oServerNames = oSQLApp.ListAvailableSQLServers()

        For i = 0 To oServerNames.Count

            strServer = (oServerNames(i))
            ServerArr = strServer.Split("\")

            txtSQLHost.Text = ServerArr(0)

            For Each s As String In ServerArr
                'value of server instance
                cbxSQLServerNames.Items.Add(s)
            Next

        Next i
    End Sub

这很有效,我很确定这不是最好的方法

组合框仅填充sqlInstances txtbox有服务器

oServerNames是server \ sqlInstance

Private Sub GetSQLServers()

        Dim oSQLApp As New SQLDMO.Application
        Dim oServerNames As SQLDMO.NameList
        Dim strServer As String
        Dim ServerArr() As String
        Dim sn As Integer = 1

        oServerNames = oSQLApp.ListAvailableSQLServers()

        For i = 0 To oServerNames.Count

            strServer = (oServerNames(i))
            ServerArr = strServer.Split("\")

            txtSQLHost.Text = ServerArr(0)

            For Each s As String In ServerArr

                'value of server instance
                If ((sn Mod 2) = 0) Then
                    cbxSQLServerNames.Items.Add(s)
                    sn = sn + 1
                Else
                    sn = sn + 1
                End If
            Next
        Next i
    End Sub

现在使用它.. 感谢

Private Sub GetSQLServers()

        Dim oSQLApp As New SQLDMO.Application
        Dim oServerNames As SQLDMO.NameList
        Dim strServer As String
        Dim ServerArr() As String

        oServerNames = oSQLApp.ListAvailableSQLServers()

        For i = 0 To oServerNames.Count

            strServer = (oServerNames(i))
            ServerArr = strServer.Split("\")

            txtSQLHost.Text = ServerArr(0)

           For Each item In oServerNames
                Dim parts = item.Split("\")
                Dim instance = parts(1)
                cbxSQLServerNames.Items.Add(instance)
            Next

        Next i
    End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

    For i = 0 To oServerNames.Count - 1 Step 2
        Dim serverInstance = oServerNames(i) & "\" & oServerNames(i + 1)
        cbxSQLServerNames.Items.Add(serverInstance)
    Next i

Count - 1因为,如果你算上10个项目,那你就是从0到9。

在查看问题的来源后,您说阵列看起来像这样:

(0) server
(0) sqlInstance
(1) server
(1) sqlInstance
(2) server
(2) sqlInstance

这是不可能的。不能有多个具有相同索引的项目。这里缺少一些东西......


如果数组看起来像这样:

(0) server\sqlInstance
(1) server\sqlInstance
(2) server\sqlInstance

你可以这样做:

For Each item In oServerNames
    Dim parts = item.Split("\")
    dim instance = parts(1)
    cbxSQLServerNames.Items.Add(instance)
Next