ServerConnection.ExecuteNonQuery Method (StringCollection, ExecutionTypes)

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

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

構文

'宣言
Public Function ExecuteNonQuery ( _
    sqlCommands As StringCollection, _
    executionType As ExecutionTypes _
) As Integer()
public int[] ExecuteNonQuery (
    StringCollection sqlCommands,
    ExecutionTypes executionType
)
public:
array<int>^ ExecuteNonQuery (
    StringCollection^ sqlCommands, 
    ExecutionTypes executionType
)
public int[] ExecuteNonQuery (
    StringCollection sqlCommands, 
    ExecutionTypes executionType
)
public function ExecuteNonQuery (
    sqlCommands : StringCollection, 
    executionType : ExecutionTypes
) : int[]

パラメータ

  • sqlCommands
    実行する Transact-SQL ステートメントのバッチを示す StringCollection システム オブジェクト値です。
  • executionType
    Transact-SQL ステートメントの実行オプションを示す ExecutionTypes オブジェクト値です。

戻り値

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 例外が発生します。

この名前空間、クラス、またはメンバは、Microsoft .NET Framework Version 2.0 でのみサポートされています。

使用例

'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

スレッド セーフ

この型の public static (Microsoft Visual Basic では共有 ) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

プラットフォーム

開発プラットフォーム

サポートされているプラットフォームの一覧については、「SQL Server 2005 のインストールに必要なハードウェアおよびソフトウェア」を参照してください。

対象プラットフォーム

サポートされているプラットフォームの一覧については、「SQL Server 2005 のインストールに必要なハードウェアおよびソフトウェア」を参照してください。

参照

関連項目

ServerConnection Class
ServerConnection Members
Microsoft.SqlServer.Management.Common Namespace

その他の技術情報

Visual Basic .NET でキャプチャ モードを有効にする方法
キャプチャ モードの使用