次の方法で共有


ExecuteNonQuery メソッド (String)

結果セットを返さないステートメントを実行します。

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

構文

'宣言
Public Function ExecuteNonQuery ( _
    sqlCommand As String _
) As Integer
'使用
Dim instance As ServerConnection
Dim sqlCommand As String
Dim returnValue As Integer

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

パラメーター

戻り値

型: System. . :: . .Int32
UPDATE ステートメント、INSERT ステートメント、および DELETE ステートメントに対応する Transact-SQL コマンドの影響を受ける行の合計数を示す Int32 値です。他のすべての種類のステートメントでは、戻り値は -1 です。

説明

Transact-SQL コマンドは、サーバーの設定に影響を与えるデータ定義言語 (DDL) ステートメントまたはストアド プロシージャであることが普通です。CapturedSql オブジェクトの Text プロパティは、キャプチャされた Transact-SQL ステートメントの実行を許可するための sqlCommand パラメーターとして使用できます。sqlCommand パラメーターは、単一の Transact-SQL ステートメントを格納します。

ExecuteNonQuery メソッドによって認識されるのは SQLCMD コマンドのみです。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