Background Information :
I have an application that makes multiple SQL connections to multiple databases, which currently take a lot of time.
Powershell (.NET) will wait for each SQL-GET function to complete before it can launch the next. I have the impression that I can speed up this application by drastically activating every SQL-GET function in my own background task! Then I will extract data from each job as it is completed. Ideal as a system object DataSet.
The problems are :
When retrieving data from a background job, I can ONLY get a System.Array object . In fact, I am, this is a System.DataSet object . This is necessary because all the logic in the application depends on the DataSet.
Code :
Here is a simple piece of code that will create a sql connection and populate a newly created dataset object with the returned results. Works. The result of $ is a DataSet, and I can handle it well.
$query = "SELECT * FROM [database]..[table] WHERE column = '123456'"
$Connection = New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server='SERVER';Database='DATABASE';User ID='SQL_USER';Password='SQL_PASSWORD'"
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = New-Object system.Data.SqlClient.SqlCommand($Query,$Connection)
$Adapter = New-Object system.Data.SqlClient.SqlDataAdapter
$Adapter.SelectCommand = $Command
$Connection.Close()
[System.Data.SqlClient.SqlConnection]::ClearAllPools()
$results = New-Object system.Data.DataSet
[void]$Adapter.fill($results)
$results.Tables[0]
And here is VERY SAME CODE wrapped in the scriptblock parameter of the new background job. Only after calling Receive-Job do I get the array back, not a dataset.
$test_job = Start-Job -ScriptBlock {
$query = "SELECT * FROM [database]..[table] WHERE column = '123456'"
$Connection = New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server='SERVER';Database='DATABASE';User ID='SQL_USER';Password='SQL_PASSWORD'"
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = New-Object system.Data.SqlClient.SqlCommand($Query,$Connection)
$Adapter = New-Object system.Data.SqlClient.SqlDataAdapter
$Adapter.SelectCommand = $Command
$Connection.Close()
[System.Data.SqlClient.SqlConnection]::ClearAllPools()
$results = New-Object system.Data.DataSet
[void]$Adapter.fill($results)
return $results.Tables[0]
}
Wait-Job $test_job
$ret_results = Receive-Job $test_job
Any help would be greatly appreciated.
Research in this way :
Google, , , , . powershell -?
. , - . :
return $results.Tables[0]return ,$results.Tables[0]return ,$results
.
, , , "" - , !:)
:
, , , $ret_results, Receive-Job, DataSet... DataTable. ... :)