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
25 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

16 Experts available now in Live!

Get 1:1 Help Now