Solved

with powershell i'm passing queries to sql in a loop.  how can i cumulate what i'm receiving from sql (datatables) ?

Posted on 2016-11-18
2
55 Views
Last Modified: 2016-11-28
hello,

in a loop i'm passing queries to sql. ok, i'm receiving the responses.
now i'm trying to group in a datatable all the respoinses i'm receiving which are datatables.
i've created the same datatable in order to be able to add to it but this is not working.

i need help.

$allmyResulst =
    $allResults = New-Object System.Data.DataTable
    $allResults.Columns.Add("PersonID",[System.Type]::GetType("System.Int32"))
    $allResults.Columns.Add("firstname",[System.Type]::GetType("System.String"))
    $allResults.Columns.Add("lastname",[System.Type]::GetType("System.String"))

foreach($aline in $allFileLines)
{
     $query = "Select PersonID, firstname, lastname, email from table where firstname='lala'
     $results = pass-queryagainstDBserver $theConnection $query
     # ^ i receive my results ok
     $allresults.rows.add($results)    <- this is not adding my new lines:
}

thank you in advance
0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41893208
Without testing, it looks like you're trying to add a DataTable to DataTable.Rows (and they're different object types).

What object type is returned by your custom function, DataTable?

Do your results have the same number of columns, because it looks like you're selecting 4 columns and adding to a table with 3.

Try this, to create a compatible row and fill it with values from a differing table, then add it back to the main table.
foreach($aline in $allFileLines)
{
     $query = "Select PersonID, firstname, lastname, email from table where firstname='lala'"
     $results = pass-queryagainstDBserver $theConnection $query 
     # ^ i receive my results ok 
     foreach ($thisRow in $results.Rows)
     {
        $addRow = $allResults.NewRow()
        $addRow["PersonID"] = $thisRow.PersonID
        $addRow["firstname"] = $thisRow.firstname
        $addRow["lastname"] = $thisRow.lastname
        $allResults.Rows.Add($addRow)
     }
}

Open in new window

0
 

Author Comment

by:toshi_
ID: 41904057
Hello Dustin,

thank you for your reply,

indeed i had forgotten to  add a column.
ok, indeed i could copy one Datarow from one Datatable to the other. Creating a new datarow works.

thank you.

Toshi
0

Featured Post

Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question