ServerConnection.ExecuteScalar 方法 (String)
執行 Transact-SQL 陳述式,並以物件值傳回第一個資料列的第一個資料行。
命名空間: Microsoft.SqlServer.Management.Common
組件: Microsoft.SqlServer.ConnectionInfo (在 Microsoft.SqlServer.ConnectionInfo.dll 中)
語法
'宣告
Public Function ExecuteScalar ( _
sqlCommand As String _
) As Object
'用途
Dim instance As ServerConnection
Dim sqlCommand As String
Dim returnValue As Object
returnValue = instance.ExecuteScalar(sqlCommand)
public Object ExecuteScalar(
string sqlCommand
)
public:
Object^ ExecuteScalar(
String^ sqlCommand
)
member ExecuteScalar :
sqlCommand:string -> Object
public function ExecuteScalar(
sqlCommand : String
) : Object
參數
- sqlCommand
型別:String
String 值,指定要執行的 Transact-SQL 命令。
傳回值
型別:Object
Object 系統物件值,指定結果集中第一個資料列的第一個資料行。
範例
C#
ServerConnection conn = new ServerConnection();
conn.DatabaseName = "AdventureWorks2012";
costData = conn.ExecuteScalar("SELECT CostRate FROM Production.Location");
Console.WriteLine(costData.ToString());
PowerShell
$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.DatabaseName = "AdventureWorks2012"
$reader = $conn.ExecuteScalar("SELECT CostRate FROM Production.Location")
Write-Host $reader