Powershell单行数据表返回填充组合框

时间:2016-08-31 20:34:03

标签: sql powershell combobox powershell-v4.0

我在Powershell v4.0中。我的UI上有一个下拉列表,我正在填充数据库中的数据。我调用一个抓取数据的函数:

$ConfigDynamicContractID.ItemsSource = (GetDynamicContracts).DefaultView

在后端它看起来像这样:

function GetDynamicContracts{
    $ContractQuery = "select con.ContractNumber as Name, c.ContractID from foo..campaign c inner join foo..campaigntemplate ct on c.campaignid = ct.campaignid
inner join foo..template t on ct.templateid = t.templateid inner join bar..contracts con on con.ContractID = c.ContractID 
    where c.ProductType = 'INTRO' and t.FileName = 'Intro Dynamic Template' and c.DeleteFlag = 0"

    $ContractResult = ExecuteSelect "Bar" $ContractQuery
    return , $ContractResult
}

此代码工作正常。然后我被告知上面引用的表不能相互通信,所以我必须对每个表单独调用并在powershell中进行过滤。所以这是我的代码:

function GetDynamicContracts{

    $CampaignQuery = "Select c.* From foo..Campaign c join foo..CampaignTemplate ct on c.CampaignID = ct.CampaignID
                    join foo..Template t on ct.TemplateID = t.TemplateID
                    Where c.ProductType = 'INTRO' and t.FileName = 'Intro Dynamic Template' and c.DeleteFlag = 0"
    $CampaignResults = ExecuteSelect "foo" $CampaignQuery

    $ContractIDs = New-Object System.Collections.ArrayList

    foreach($row in $CampaignResults.Rows){
        $ContractIDs.Add($row.ContractID)
    }
    $ContractIDs = $ContractIDs -join ","

    $PHQuery = "Select ContractID, ContractNumber as Name From Contracts Where ContractID in ($ContractIDs)"
    $PHResults = ExecuteSelect "Bar" $PHQuery

    #Log $MyInvocation.MyCommand $PSBoundParameters
    return ,$PHResults
}

由于某些原因,此代码在返回单行时不起作用。但是,如果我把这个函数调用分成两个单独的,那么它的工作原理。见下文:

function GetDynamicContracts{

    $ContractIDs = GetDynamicCampaign
    $ContractIDs = $ContractIDs[1]

    $PHQuery = "Select ContractID, ContractNumber as Name From Contracts Where ContractID in ($ContractIDs)"
    $PHResults = ExecuteSelect "PhytelMaster" $PHQuery

    Log $MyInvocation.MyCommand $PSBoundParameters
    return ,$PHResults
}

function GetDynamicCampaign{

    $CampaignQuery = "Select c.* From Campaign..Campaign c join Campaign..CampaignTemplate ct on c.CampaignID = ct.CampaignID
                    join Campaign..Template t on ct.TemplateID = t.TemplateID
                    Where c.ProductType = 'INTRO' and t.FileName = 'Intro Dynamic Template' and c.DeleteFlag = 0"
    $CampaignResults = ExecuteSelect "Campaign" $CampaignQuery

    $ContractIDs = New-Object System.Collections.ArrayList
    foreach($row in $CampaignResults.Rows){
        $ContractIDs.Add($row.ContractID)
    }
    $ContractIDs = $ContractIDs -join ","

    return $ContractIDs
}

似乎在单个函数中进行两次执行选择会导致','无法执行返回数据时的操作。谁能解释为什么?提前谢谢,因为我有一个解决方法,所以不要急于求成。

1 个答案:

答案 0 :(得分:0)

由于@TheMadTechnician和@wOxxOm指出ArrayList将索引阻塞到return和return中,返回在函数中发送垃圾邮件的所有内容。因此,当我预期$ PHResults时,我也得到了ArrayList垃圾邮件。我可以使用除ArrayList之外的其他东西,或者我也发现如果我将函数代码包装在$null = @{ 'Code' }中然后返回我想要的变量,那么我得到的就是那个变量。这很丑,但它确实有效。