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