Retrieving data using select SQL statement in Powershell

This URL helped in retrieving data from SQL query into a variable.
I would like to enhance this to retrieve multiple values.

$SqlCmd.CommandText = "select column1,column2 from table1"
$column1 = $SqlCmd.ExecuteScalar()

How can I assign column2 value into a variable.

LVL 41
SharathData EngineerAsked:
Who is Participating?
Raheman M. AbdulConnect With a Mentor Senior Infrastructure Support Analyst & Systems DeveloperCommented:
Try the following:
Executescalar returns only one value
Executereader returns multiple values
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = "select column1,column2 from table1"

$Reader = $Command.ExecuteReader()
$column1 = $Reader.GetValue(0)
$column2 = $Reader.GetValue(1)
SharathData EngineerAuthor Commented:
will get back on this
SharathData EngineerAuthor Commented:
Thanks for pointing to use Executereader. Somehow, I am getting issues with this. I used a different approach to get the work done. Thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.