ServerConnection.ExecuteReader Method (String)

Executes Transact-SQL statements that return one or multiple result sets.

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

public SqlDataReader ExecuteReader(
	string sqlCommand
)

Parameters

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

Return Value

Type: System.Data.SqlClient.SqlDataReader
A SqlDataReader object value that contains one or more result sets.

The SqlDataReader object is returned to let the client process the data without requiring it to read all information into memory.

C#

ServerConnection conn = new ServerConnection();
conn.DatabaseName = "AdventureWorks2008R2";
SqlDataReader reader = conn.ExecuteReader("SELECT Name, CostRate FROM Production.Location");
while (reader.Read())
{
   string name = (string)reader["Name"];
   decimal cRate = (decimal)reader["CostRate"];
   Console.WriteLine(name + "(" + cRate + ")");
}
reader.Close();

PowerShell

$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.DatabaseName = "AdventureWorks2008R2"
$reader = $conn.ExecuteReader("SELECT Name, CostRate FROM Production.Location")
while ($reader.Read())
{
   $name = $reader.GetValue(0);
   $cRate = $reader.GetValue(1);
   Write-Host $name,"(",$cRate,")"
}
$reader.Close()

Community Additions

ADD
Show: