Share via


ExecuteNonQuery メソッド (StringCollection, ExecutionTypes)

指定された実行オプションで、結果セットを返さないステートメントのバッチを実行します。

名前空間:  Microsoft.SqlServer.Management.Common
アセンブリ:  Microsoft.SqlServer.ConnectionInfo (Microsoft.SqlServer.ConnectionInfo.dll)

構文

'宣言
Public Function ExecuteNonQuery ( _
    sqlCommands As StringCollection, _
    executionType As ExecutionTypes _
) As Integer()
'使用
Dim instance As ServerConnection
Dim sqlCommands As StringCollection
Dim executionType As ExecutionTypes
Dim returnValue As Integer()

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

パラメーター

戻り値

型: array<System. . :: . .Int32> [] () [] []
sqlCommands パラメーターとして使用される StringCollection オブジェクトの各要素の影響を受ける行の合計数を示す Int32 配列の値です。戻り値は、UPDATE ステートメント、INSERT ステートメント、および DELETE ステートメントに対応する Transact-SQL コマンドの影響を受ける行の合計数を示します。他のすべての種類のステートメントでは、戻り値は -1 です。

説明

通常は結果セットを返さない Transact-SQL ステートメントのバッチを実行します。このようなステートメントは、サーバーの設定に影響を与えるデータ定義言語 (DDL) ステートメントまたはストアド プロシージャであることが普通です。

CapturedSql オブジェクトの Text プロパティは、キャプチャされた Transact-SQL ステートメントの実行を許可するためのパラメーターとして使用できます。

sqlCommands パラメーターとして使用する StringCollection オブジェクトには、バッチ区切り記号で区切って複数のバッチを格納することができます。バッチ区切り記号は、既定では GO ですが、BatchSeparator プロパティで設定することもできます。バッチの実行は、executionType パラメーターで制御されます。

ExecuteNonQuery メソッドによって認識されるのは SQLCMD コマンドのみです。sqlCommand パラメーターに SQLCMD コマンド以外のステートメントが格納されている場合、ExecutionTypes.ContinueOnError 値を含めるように executionType パラメーターが設定されていない限り、メソッドは失敗し、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