Поделиться через


Метод ExecuteScalar (String)

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

Пространство имен:  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

Параметры

Возвращаемое значение

Тип: System. . :: . .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 = "AdventureWorks2008R2";
costData = conn.ExecuteScalar("SELECT CostRate FROM Production.Location");
Console.WriteLine(costData.ToString());

PowerShell

$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.DatabaseName = "AdventureWorks2008R2"
$reader = $conn.ExecuteScalar("SELECT CostRate FROM Production.Location")
Write-Host $reader