ServerConnection.ExecuteWithResults Method (String)

Executes a Transact-SQL statement that returns a result set.

Namespace:  Microsoft.SqlServer.Management.Common
Assembly:  Microsoft.SqlServer.ConnectionInfo (in Microsoft.SqlServer.ConnectionInfo.dll)

public DataSet ExecuteWithResults(
	string sqlCommand
)

Parameters

sqlCommand
Type: System.String
A String value that specifies the Transact-SQL command to be executed.

Return Value

Type: System.Data.DataSet
A DataSet system object value that contains the result set.

C#

ServerConnection conn = new ServerConnection();
conn.DatabaseName = "AdventureWorks2008R2";
DataSet costData = conn.ExecuteWithResults("SELECT Name, CostRate FROM Production.Location");
DataTable infoTable = costData.Tables[0];
foreach (DataRow r in infoTable.Rows)
{
   Console.WriteLine("==========================");
   foreach (DataColumn c in infoTable.Columns)
   {
       Console.WriteLine(c.ColumnName + " = " + r[c].ToString());
   }
}

PowerShell

$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.DatabaseName = "AdventureWorks2008R2"
$costData = $conn.ExecuteWithResults("SELECT Name, CostRate FROM Production.Location")
$infoTable = $costData.Tables[0]
foreach ($r in $infoTable.Rows)
{
   Write-Host "=========================="
   foreach ($c in $infoTable.Columns)
   {
      Write-Host $c.ColumnName "=" $r[$c].ToString()
   }
}

Community Additions

ADD
Show: