Created
July 4, 2017 04:40
-
-
Save develohpanda/0c6ac41fab64a7160dd2368f8f775054 to your computer and use it in GitHub Desktop.
Run SQL from Powershell
This file contains hidden or 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
function Run-SqlCommand($serverName, $databaseName, $command, $timeoutSeconds = 30) { | |
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection | |
$SqlConnection.ConnectionString = "Server=$serverName;Database=$databaseName;Integrated Security=True" | |
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand | |
$SqlCmd.CommandText = $command | |
$SqlCmd.Connection = $SqlConnection | |
$SqlCmd.CommandTimeout = $timeoutSeconds | |
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter | |
$SqlAdapter.SelectCommand = $SqlCmd | |
$DataSet = New-Object System.Data.DataSet | |
$SqlAdapter.Fill($DataSet) | |
$SqlConnection.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment