SMO プログラムは、プログラムによって実行されるステートメントの代わりに、またはプログラムによって実行されるステートメントに加えて、同等の Transact-SQL ステートメントを取り込んで記録できます。 キャプチャ モードを有効にするには、ServerConnection オブジェクトを使用するか、Server オブジェクトの ConnectionContext プロパティを使用します。
例
提供されているコード例を使用するには、プログラミング環境、プログラミング テンプレート、およびアプリケーションを作成するプログラミング言語を選択する必要があります。 詳細については、SQL Server オンライン ブックの「方法: Visual Studio .NET で Visual Basic SMO プロジェクトを作成する」または「方法: Visual Studio .NET で Visual C# SMO プロジェクトを作成する」を参照してください。
Visual Basic でのキャプチャ モードの有効化
このコード例では、キャプチャ モードを有効にし、キャプチャ バッファーに保持されている Transact-SQL コマンドを表示します。
Visual C でのキャプチャ モードの有効化#
このコード例では、キャプチャ モードを有効にし、キャプチャ バッファーに保持されている Transact-SQL コマンドを表示します。
{
// Connect to the local, default instance of SQL Server.
Server srv;
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.
string s;
foreach ( String p_s in srv.ConnectionContext.CapturedSql.Text ) {
Console.WriteLine(p_s);
}
// Execute the captured statements.
srv.ConnectionContext.ExecuteNonQuery(srv.ConnectionContext.CapturedSql.Text);
// Revert to immediate execution mode.
srv.ConnectionContext.SqlExecutionModes = SqlExecutionModes.ExecuteSql;
}