Export (0) Print
Expand All

ServerConnection.ExecuteScalar Method (String)

Executes a Transact-SQL statement and returns the first column of the first row as an object value.

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

public Object ExecuteScalar(
	string sqlCommand
)

Parameters

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

Return Value

Type: Object
An Object system object value that specifies the first column of the first row of the result set.

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

Community Additions

Show:
© 2015 Microsoft