Created
June 14, 2013 10:03
-
-
Save mintsoft/5780780 to your computer and use it in GitHub Desktop.
Execute TSQL against a SQL Server from Powershell without SQL Server Management Studio modules installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$connectionString = "Data Source=cidbsql.cwserverfarm.local;Initial Catalog=workdb;Integrated Security=True;Application Name=PowerSheQL"; | |
$SQL = "SELECT TOP 5 * FROM dbo.RE_Trace;"; | |
$dbc = New-Object System.Data.SqlClient.SqlConnection($connectionString); | |
$dbc.Open(); | |
$sqlcmd = $dbc.CreateCommand(); | |
$sqlcmd.CommandTimeout = 1; | |
$sqlcmd.CommandText = $SQL; | |
$sqlcmd.Prepare(); | |
$sqlReader = $sqlcmd.ExecuteReader(); | |
$Datatable = New-Object System.Data.DataTable; | |
$Datatable.Load($SqlReader); | |
$dbc.Close(); | |
$Datatable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment