2014-03-07 112 views
0

我試圖填充組合框與陣列的所有其他元素的所有其他價值得到字符串數組

數組包含這樣

(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 
+0

使用列表(類),數組的:缺少的東西在這裏...


如果數組是這樣的:

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

你可以這樣做那麼無論你想要什麼,你都可以發佈你想要的任何元素。 – Plutonix

回答

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 

這是不可能的。不能有多個具有相同索引的項目。而不是

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

這將返回server \ sqlInstance \ server \ sqlInstance \ server \ sqlInstance –

+0

它不應該返回任何內容,但會將多個「server \ sqlInstance」添加到組合框。我誤解了一些東西嗎? - 是的,在多次閱讀您的問題後,我不認爲我理解了數組的內容。你能更清楚地解釋一下數組究竟包含什麼? – dummy

+0

數組輸入是server \ sqlInstance。分裂後(「\」)它是(0)服務器(0)實例 –