Skip to content

Instantly share code, notes, and snippets.

@Warrenn
Created August 17, 2022 18:26
Show Gist options
  • Save Warrenn/d6b344d31e7ec63a08423a84f00fb345 to your computer and use it in GitHub Desktop.
Save Warrenn/d6b344d31e7ec63a08423a84f00fb345 to your computer and use it in GitHub Desktop.
get a dataset from a database
function Get-DataSet($sqlCommand, $parameters, $connectionString) {
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$dataset = New-Object System.Data.DataSet
$parameters | Select-Object -ExpandProperty Keys | %{
$command.Parameters.Add($_, $parameters[$_])
}
try{
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$adapter.Fill($dataSet) | Out-Null
}
finally {
$connection.Close()
}
return $dataSet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment