ServerConnection.ExecuteReader 方法 (String)
執行傳回一個或多個結果集的 Transact-SQL 陳述式。
命名空間: Microsoft.SqlServer.Management.Common
組件: Microsoft.SqlServer.ConnectionInfo (在 Microsoft.SqlServer.ConnectionInfo.dll 中)
語法
'宣告
Public Function ExecuteReader ( _
sqlCommand As String _
) As SqlDataReader
'用途
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
參數
- sqlCommand
型別:String
String 值,指定要執行的 Transact-SQL 命令。
傳回值
型別:SqlDataReader
包含一個或多個結果集的 SqlDataReader 物件值。
備註
SqlDataReader 物件傳回讓用戶端資料,而不需要讀取所有資訊載入記憶體。
範例
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()