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
12 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_
2 Comments
 
LVL 12

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now