共用方式為


ServerConnection.ExecuteWithResults 方法 (String)

執行傳回結果集的 Transact-SQL 陳述式。

命名空間:  Microsoft.SqlServer.Management.Common
組件:  Microsoft.SqlServer.ConnectionInfo (在 Microsoft.SqlServer.ConnectionInfo.dll 中)

語法

'宣告
Public Function ExecuteWithResults ( _
    sqlCommand As String _
) As DataSet
'用途
Dim instance As ServerConnection 
Dim sqlCommand As String 
Dim returnValue As DataSet 

returnValue = instance.ExecuteWithResults(sqlCommand)
public DataSet ExecuteWithResults(
    string sqlCommand
)
public:
DataSet^ ExecuteWithResults(
    String^ sqlCommand
)
member ExecuteWithResults : 
        sqlCommand:string -> DataSet
public function ExecuteWithResults(
    sqlCommand : String
) : DataSet

參數

  • sqlCommand
    型別:String
    String 值,指定要執行的 Transact-SQL 命令。

傳回值

型別:DataSet
包含結果集的 DataSet 系統物件值。

範例

C#

ServerConnection conn = new ServerConnection();
conn.DatabaseName = "AdventureWorks2012";
DataSet costData = conn.ExecuteWithResults("SELECT Name, CostRate FROM Production.Location");
DataTable infoTable = costData.Tables[0];
foreach (DataRow r in infoTable.Rows)
{
   Console.WriteLine("==========================");
   foreach (DataColumn c in infoTable.Columns)
   {
       Console.WriteLine(c.ColumnName + " = " + r[c].ToString());
   }
}

PowerShell

$conn = new-object Microsoft.SqlServer.Management.Common.ServerConnection
$conn.DatabaseName = "AdventureWorks2012"
$costData = $conn.ExecuteWithResults("SELECT Name, CostRate FROM Production.Location")
$infoTable = $costData.Tables[0]
foreach ($r in $infoTable.Rows)
{
   Write-Host "=========================="
   foreach ($c in $infoTable.Columns)
   {
      Write-Host $c.ColumnName "=" $r[$c].ToString()
   }
}

請參閱

參考

ServerConnection 類別

ExecuteWithResults 多載

Microsoft.SqlServer.Management.Common 命名空間