Share via


ServerConnection.ExecuteNonQuery 方法 (StringCollection)

執行不會傳回結果集的陳述式批次。

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

語法

'宣告
Public Function ExecuteNonQuery ( _
    sqlCommands As StringCollection _
) As Integer()
'用途
Dim instance As ServerConnection 
Dim sqlCommands As StringCollection 
Dim returnValue As Integer()

returnValue = instance.ExecuteNonQuery(sqlCommands)
public int[] ExecuteNonQuery(
    StringCollection sqlCommands
)
public:
array<int>^ ExecuteNonQuery(
    StringCollection^ sqlCommands
)
member ExecuteNonQuery : 
        sqlCommands:StringCollection -> int[] 
public function ExecuteNonQuery(
    sqlCommands : StringCollection
) : int[]

參數

  • sqlCommands
    型別:StringCollection
    StringCollection 系統物件值,指定要執行的 Transact-SQL 陳述式批次。

傳回值

型別:array<Int32[]
Int32 陣列值,指定受到 StringCollection 物件的每個元素 (當做 sqlCommands 參數) 影響的資料列總數。傳回值指定受到 INSERT、UPDATE 和 DELETE 陳述式之 Transact-SQL 命令影響的資料列總數。對於所有其他類型的陳述式,傳回值為 -1

備註

執行批次通常不會傳回結果集的 Transact-SQL 陳述式。 這些通常是資料定義語言 (DDL) 影響伺服器設定 (DDL) 陳述式或預存程序。

CapturedSql 物件的 Text 屬性可用來作為參數可將擷取的 Transact-SQL 陳述式的執行。

為 sqlCommands 參數使用的 StringCollection 物件可能包含批次分隔符號來分隔多個批次。 根據預設,批次分隔符號是 GO,,但是可以設定與 BatchSeparator 屬性。 批次執行控制與 executionType 參數。

只有 SQLCMD 命令可 ExecuteNonQuery 方法辨識。 如果 sqlCommand 參數包含非 SQLCMD 命令接著方法失敗或 ExecutionFailureException 例外狀況的陳述式而引發。

範例

VB

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Set the execution mode to CaptureSql for the connection.
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.CaptureSql
'Make a modification to the server that is to be captured.
srv.UserOptions.AnsiNulls = True
srv.Alter()
'Iterate through the strings in the capture buffer and display the captured statements.
Dim s As String
For Each s In srv.ConnectionContext.CapturedSql.Text
    Console.WriteLine(s)
Next
'Execute the captured statements.
srv.ConnectionContext.ExecuteNonQuery(srv.ConnectionContext.CapturedSql.Text)
'Revert to immediate execution mode. 
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql

PowerShell

$srv = new-object Microsoft.SqlServer.Management.Smo.Server
$srv.ConnectionContext.SqlExecutionModes = [Microsoft.SqlServer.Management.Common.SqlExecutionModes]::CaptureSql
$srv.UserOptions.AnsiNulls = $TRUE
$srv.Alter()
foreach ($s in $srv.ConnectionContext.CapturedSql.Text)
{
   Write-Host $s
}
$srv.ConnectionContext.ExecuteNonQuery($srv.ConnectionContext.CapturedSql.Text)
$srv.ConnectionContext.SqlExecutionModes = [Microsoft.SqlServer.Management.Common.SqlExecutionModes]::ExecuteSql

請參閱

參考

ServerConnection 類別

ExecuteNonQuery 多載

Microsoft.SqlServer.Management.Common 命名空間

其他資源

使用擷取模式