DaveWentzel.com            All Things Data

Run Any Generic SQL Query

 

function RunQuery(

[string] $servername

)

{

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter

$DataSet = New-Object System.Data.DataSet

$SqlConnection.ConnectionString =

    "Server=$servername;Database=master;Integrated Security=True"

$SqlCmd.CommandText = "SELECT * FROM sys.processes"

$SqlCmd.Connection = $SqlConnection

$SqlAdapter.SelectCommand = $SqlCmd

$SqlAdapter.Fill($DataSet)|out-null

$dbs =$DataSet.Tables[0]

$dbs



$SqlConnection.Close()

}
 
 
 

Add new comment