Created
September 22, 2015 08:58
-
-
Save cmatskas/26d41eb44c22b18ac225 to your computer and use it in GitHub Desktop.
PowershellSqlCommandDataAdapter.ps1
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
[string] $Server= ".\SQLEXPRESS2014" | |
[string] $Database = "MyDatabase" | |
[string] $UserSqlQuery= $("SELECT * FROM [dbo].[User]") | |
$resultsDatatable = ExecuteSqlQuery $Server $Database $UserSqlQuery | |
function GenericSqlQuery ($Server, $Database, $SQLQuery) { | |
$Datatable = New-Object System.Data.DataTable | |
$Connection = New-Object System.Data.SQLClient.SQLConnection | |
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;" | |
$Connection.Open() | |
$Command = New-Object System.Data.SQLClient.SQLCommand | |
$Command.Connection = $Connection | |
$Command.CommandText = $SQLQuery | |
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $Command | |
$Dataset = new-object System.Data.Dataset | |
$DataAdapter.Fill($Dataset) | |
$Connection.Close() | |
return $Dataset.Tables[0] | |
} | |
#validate we got data | |
Write-Host ("The table contains: " + $resultsDatatable.Rows.Count + " rows") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment