Function Query-SQLDB
{
<#
.Synopsis
This function connects and queries given SQL DB
.Parameter SQLServer
name of the SQL servert to connect to
.Parameter SQLDBName
name of the DB to query for
.Parameter SQLQuery
SQL query
.Example
Query-SQLDB
#>
[CmdletBinding()]
Param(
$SQLServer="sqlserver",
$SQLDBName="testDB",
$SQLQuery="select * from testTB where UID=1"
)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQLQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) > $null
[void]$SqlConnection.Close()
return $DataSet
}