Partager via


Méthode ServerConnection.ExecuteReader (String)

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

Espace de noms :  Microsoft.SqlServer.Management.Common
Assembly :  Microsoft.SqlServer.ConnectionInfo (en Microsoft.SqlServer.ConnectionInfo.dll)

Syntaxe

'Déclaration
Public Function ExecuteReader ( _
    sqlCommand As String _
) As SqlDataReader
'Utilisation
Dim instance As ServerConnection 
Dim sqlCommand As String 
Dim returnValue As SqlDataReader 

returnValue = instance.ExecuteReader(sqlCommand)
public SqlDataReader ExecuteReader(
    string sqlCommand
)
public:
SqlDataReader^ ExecuteReader(
    String^ sqlCommand
)
member ExecuteReader : 
        sqlCommand:string -> SqlDataReader
public function ExecuteReader(
    sqlCommand : String
) : SqlDataReader

Paramètres

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

Valeur de retour

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

Notes

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

Exemples

C#

ServerConnection conn = new ServerConnection();
conn.DatabaseName = "AdventureWorks2012";
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 = "AdventureWorks2012"
$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()

Voir aussi

Référence

ServerConnection Classe

Surcharge ExecuteReader

Espace de noms Microsoft.SqlServer.Management.Common