ServerConnection.ExecuteScalar 方法 (StringCollection)
執行 Transact-SQL 陳述式的批次,並以物件值傳回第一個資料列的第一個資料行。
命名空間: Microsoft.SqlServer.Management.Common
組件: Microsoft.SqlServer.ConnectionInfo (在 Microsoft.SqlServer.ConnectionInfo.dll 中)
語法
'宣告
Public Function ExecuteScalar ( _
sqlCommands As StringCollection _
) As Object()
'用途
Dim instance As ServerConnection
Dim sqlCommands As StringCollection
Dim returnValue As Object()
returnValue = instance.ExecuteScalar(sqlCommands)
public Object[] ExecuteScalar(
StringCollection sqlCommands
)
public:
array<Object^>^ ExecuteScalar(
StringCollection^ sqlCommands
)
member ExecuteScalar :
sqlCommands:StringCollection -> Object[]
public function ExecuteScalar(
sqlCommands : StringCollection
) : Object[]
參數
- sqlCommands
型別:StringCollection
StringCollection 系統物件值,指定要執行的 Transact-SQL 陳述式批次。
傳回值
型別:array<Object[]
物件陣列值,指定每個結果集中第一個資料列的第一個資料行。
範例
C#
ServerConnection conn = new ServerConnection();
conn.DatabaseName = "AdventureWorks2012";
StringCollection sqlQuery = new StringCollection();
sqlQuery.Add("SELECT Name FROM Production.Location");
sqlQuery.Add("SELECT CostRate FROM Production.Location");
Object[] costData = conn.ExecuteScalar(sqlQuery);
for (int i = 0; i < costData.Length; i++)
{
Console.WriteLine(costData[i].ToString());
}
PowerShell
$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.DatabaseName = "AdventureWorks2012"
$sqlQuery = new-object System.Collections.Specialized.StringCollection
$sqlQuery.Add("SELECT Name FROM Production.Location")
$sqlQuery.Add("SELECT CostRate FROM Production.Location")
$costData = $conn.ExecuteScalar($sqlQuery)
$i = 0
while ($i -le $costData.Length)
{
Write-Host $costData[$i]
$i++
}
請參閱
參考
Microsoft.SqlServer.Management.Common 命名空間