Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Powershell: SQLCMD, substituting variable for -serverinstance parameter

Status
Not open for further replies.

mark Dickinson

Technical User
Jul 10, 2020
1
US

I need to get the results of the first DROPDOWN box stored as a variable, and used in place of INSTANCE_NAME in the second DROPDOWN box.

Essentially, the SELECT query from the second box needs to be run against the MASTER database in the SLQ_INSTANCE that the first DROPDOWN box results in once a choice has been made.

I've tried a few variations and can't seem to get it right. Any suggestions?

Thanks in advance!


Code:
#first DROPDOWN Box
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'SQL Server Name'
$form.Controls.Add($label)
$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(10,40)
$DropDownBox.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox.DropDownHeight = 200
$Form.Controls.Add($DropDownBox)
$wksList= invoke-sqlcmd -query "select * from VIEW_NAME
order by instance_name" -database DATABASE_NAME -serverinstance INSTANCE_NAME
foreach ($wks in $wksList) {
                      $DropDownBox.Items.Add($wks.Instance_Name)
                              } #end foreach
#end first DROPDOWN box

#second DROPDOWN Box
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,90)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Database Name'
$form.Controls.Add($label2)
$DropDownBox2 = New-Object System.Windows.Forms.ComboBox
$DropDownBox2.Location = New-Object System.Drawing.Size(10,110)
$DropDownBox2.Size = New-Object System.Drawing.Size(260,20)
$DropDownBox2.DropDownHeight = 200
$Form.Controls.Add($DropDownBox2)
$wksList2= invoke-sqlcmd -query "select name from sys.databases
where database_id>4
order by name" -database MASTER -serverinstance INSTANCE_NAME
foreach ($wks in $wksList2) {
                     $DropDownBox2.Items.Add($wks.name)
                            } #end foreach
#end second DROPDOWN box
 
Hi you seem to be using WPF forms so this may work..

Code:
function BuildDropDown1 { 
    $YearFolders = Get-ChildItem -path $GalleryFolder.Text -Filter 2* 
    $combobox0.Items.Clear()
    foreach ($SubFolder in $YearFolders)
    {
        $WhatYear = "{0}" -f $SubFolder.Name
        $combobox0.items.add("$WhatYear") 
    }
}

function BuildDropDown2 {
     $Gallery = -join($GalleryFolder.Text,$combobox0.text,"\")
     $GalleryFolders = get-childitem -path  $Gallery -Directory
     $combobox1.Items.Clear()
    foreach ($GalFolder in $GalleryFolders)
    {
        $WhatFolder = "{0}" -f $GalFolder.Name
        $combobox1.items.add("$WhatFolder") 
    }
}

triggered by
Code:
$GalleryFolder.Add_TextChanged({ BuildDropDown1 })
$GallerySubFolder.Add_SelectedIndexChanged({  BuildDropDown2  })

This isnt using SQL but can probably be adapted but runs a dir against a folder, then populates the 2nd box with the filtered names (2*) of the subfolders into the second dropdown, then results of the subfolders inside them in a 3rd drop down..?

This example works on am image gallery folder structure similat to

2010
Gallery 1​
Gallery 2​
2011
Gallery 1​
Gallery 2​
etc.

PaulSc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top