How to get this SQL query working in Powershell

The query is working in SQL but it doesnt give me the same output in Powershell

$dataSource = "\sqlexpress"
$user = "*****"
$pwd = "*****"
$database = "test bv"
 
$connectionString = "Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;"
 
$query = "SELECT De2EngelenDb.dbo.mwOrders.fldStatus, De2EngelenDb.dbo.mwOrders.fldDatum, De2EngelenDb.dbo.mwOrderRegels.fldArtikelID, tblArtikel.fldOmschrijving,
                      De2EngelenDb.dbo.mwOrders.fldFactNr, tblRelatie.fldNaam
FROM tblRelatie INNER JOIN
                      De2EngelenDb.dbo.mwOrders ON tblRelatie.fldRelatieID = De2EngelenDb.dbo.mwOrders.fldRelatieID INNER JOIN
                      De2EngelenDb.dbo.mwOrderRegels ON De2EngelenDb.dbo.mwOrders.fldOrderID = De2EngelenDb.dbo.mwOrderRegels.fldOrderID INNER JOIN
                      tblArtikel ON De2EngelenDb.dbo.mwOrderRegels.fldArtikelID = tblArtikel.fldArtikelID
WHERE     (De2EngelenDb.dbo.mwOrders.fldStatus < 50)
ORDER BY De2EngelenDb.dbo.mwOrders.fldDatum DESC"
 
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
#$connection.ConnectionString = "Server=$dataSource;Database=$database;Integrated Security=True;"
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText  = $query
 
$result = $command.ExecuteReader()

Thx for your help

Danny
LVL 6
Danny KonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
The code itself seems right, but I have noticed some things that may be causing the issue.
It seems that you have not added the dot (.) at the beggining of the SQLEXPRESS datasource.
$dataSource = ".\SQLEXPRESS" 

Open in new window


Also, I would double check if the database name is  correct, because you have a space on the name. Maybe the space does not exist or is a underline.
$database = "test_bv" 

Open in new window

or
$database = "testbv" 

Open in new window

0
Deepak ChauhanSQL Server DBACommented:
Hi Danny, Can you elaborate more what is the difference in output. Is this giving error or only result difference.
0
Rainer JeschorCommented:
Hi Danny,
does it return anything in Powershell?
Because your statement "the same output" could indicate that not everything is returned.

Am I right that your query addresses multiple databases?

Thanks.
Rainer
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Danny KonAuthor Commented:
Walter. Sorry i didnt explain, i changed the information in de database/datasource myself, that is not the problem
If i do the query in SQL i get the information i want if i do it in Powershell i see only:


         
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6
                                                                                                                                                                     6

Thanks for helping Danny
0
Walter RitzelSenior Software EngineerCommented:
No problem.
Please publish the code that prints the result in powershell
0
Danny KonAuthor Commented:
Rainer,

You are right the same question as you answered before but now is the query more complex.
The output i get is in my answer to Walter

Danny
0
Danny KonAuthor Commented:
Walter,

At this moment only
$result
0
Walter RitzelSenior Software EngineerCommented:
I think you need this:
$table = new-object “System.Data.DataTable”
$table.Load($result)
$format = @{Expression={$_.fldStatus};Label=”fldStatus”},@{Expression={$_.fldDatum};Label=”fldDatum”},@{Expression={$_.fldArtikelID};Label=”fldArtikelID”},@{Expression={$_.fldOmschrijving};Label=”fldOmschrijving”},@{Expression={$_.fldFactNr};Label=”fldFactNr”},@{Expression={$_.fldNaam};Label=”fldNaam”}
$table | format-table $format

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Danny KonAuthor Commented:
Walter,

You think 100% correct

Exactly what the patient needed:)

Thanks Danny
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.