Skip to content

Instantly share code, notes, and snippets.

@isaksky
Created May 5, 2017 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaksky/3c3397fc29964dc0573d355f6f3ad9b6 to your computer and use it in GitHub Desktop.
Save isaksky/3c3397fc29964dc0573d355f6f3ad9b6 to your computer and use it in GitHub Desktop.
Powershell Query SQL
function sq
{
param([string]$Query)
$SQLServer = "ISAK-NEW\SQLEXPRESS"
$SQLDBName = "AdventureWorks2014"
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $Query
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$ds = New-Object System.Data.DataSet
$SqlAdapter.Fill($ds) | out-null
return [management.automation.languageprimitives]::GetEnumerator($ds.Tables[0])
}
sq "SELECT * from information_schema.columns" | Out-GridView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment