Skip to content

Instantly share code, notes, and snippets.

@vaderj
Last active June 12, 2018 17:00
Show Gist options
  • Save vaderj/28c3ec83804e568078402b670f3a8377 to your computer and use it in GitHub Desktop.
Save vaderj/28c3ec83804e568078402b670f3a8377 to your computer and use it in GitHub Desktop.
PowerShell => MS SQL - just to verify that a connection is correctly setup.This is equivalent to "The UDL trick" #PowerShell #SQL #Windows
# Open a connection to the SQL Server Database Engine
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Server=SQLDB2016\SHAREPOINT;Database=master;Integrated Security=True"
$sqlConnection.Open()
# Query the master database
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandText = "SELECT name FROM [master].[sys].[databases]"
$sqlCommand.Connection = $sqlConnection
$sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlDataAdapter.SelectCommand = $sqlCommand
$dataSet = New-Object System.Data.DataSet
$sqlDataAdapter.Fill($dataSet)
# Close the SQL Server connection
$sqlConnection.Close()
# Dump out the results
$dataSet.Tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment