Strange behavior in PowerShell function returning DataSet/DataTable

前端 未结 3 812
轮回少年
轮回少年 2020-12-01 14:38

This is driving me crazy. I have a library I source from multiple scripts, which contains the following function:

function lib_open_dataset([string] $sql) {
         


        
3条回答
  •  鱼传尺愫
    2020-12-01 15:06

    Oh yes, I've been struggeling with this one too until I got this post..(tnxs Keith !)

    2 things you need to indeed focus on a) prepend your returned object with the comma indeed b) when you're filling your adaptor, make sure to either assign the outcome to a (disposalble) variable or do an Out-Null

    I didn't do the Out-Null and even with a prepended comma, I kept getting a collection back (item 0= number of rows from the query, item1= the datatable) Drove my a bit crazy until I picked the Out-null parameter out.

    Very weird IMHO, as I'm asking specifically to return the datatable but kept getting the collection back, even with the "," in front

    function  Oracleconnection
    {
      process
      {
      trap
        {
          Write-Host "error occured on oracle connection"
          Write-Host $_
          continue
        }
        [System.Reflection.Assembly]::LoadWithPartialName(“System.Data.OracleClient”) | out-null
        $connection = new-object system.data.oracleclient.oracleconnection( `
        "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=myhost.host)(PORT=1800)) `
        (CONNECT_DATA=(SERVICE_NAME=myservicename)));User Id=myid;Password=mypassword;");
    
        $query = "SELECT country, asset FROM table "
        $set = new-object system.data.dataset
        $adapter = new-object system.data.oracleclient.oracledataadapter ($query, $connection)
        $adapter.Fill($set) | Out-Null
        $table = new-object system.data.datatable
        $table = $set.Tables[0]
        return ,$table
      }
    }
    

提交回复
热议问题