SQL Select with Powershell on MSSQL

https://youtu.be/VZycEzeXKXs
Example ran against a live database.

Below are the commands used in the above video, made available to you so you can easily read through and make use of whatever you need.

$SqlQuery = @"
SELECT *
FROM TB_TEST
"@

$DataBaseName = "TESTING"

$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SQLConnection.ConnectionString="Server=localhost;Database=$DataBaseName;Integrated Security=SSPI;"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$sqlCmd.Connection = $SQLConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)

$DataSet.Tables[0]

Related posts