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

このセクションでは、Microsoft Visual Basic .NET でキャプチャ モードを有効する方法について説明します。

コードの例では、キャプチャ モードが有効化され、次にキャプチャ バッファに保持されているコマンドが表示されます。

キャプチャ モードの有効化

  1. Visual Studio 2005 を起動します。

  2. [ファイル] メニューの [新規作成] をポイントして [プロジェクト] をクリックします。[新しいプロジェクト] ダイアログ ボックスが表示されます。

  3. [プロジェクトの種類] ペインで、[Visual Basic] をクリックします。[テンプレート] ペインで、[コンソール アプリケーション] をクリックします。

  4. (省略可) [名前] ボックスに新しいアプリケーションの名前を入力します。

  5. [OK] をクリックすると、Visual Basic コンソール アプリケーション テンプレートが読み込まれます。

  6. [プロジェクト] メニューの [参照の追加] をクリックします。[参照の追加] ダイアログ ボックスが表示されます。[参照] をクリックして、C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies フォルダ内で SMO アセンブリを探します。次のファイルを選択します。

    Microsoft.SqlServer.ConnectionInfo.dll

    Microsoft.SqlServer.Smo.dll

    Microsoft.SqlServer.SqlEnum.dll

    Microsoft.SqlServer.SmoEnum.dll

  7. [表示] メニューの [コード] をクリックします。または、[Module1.vb] ウィンドウをクリックしてコード ウィンドウを表示します。

  8. コードでは、宣言の前に、次の Imports ステートメントを入力し、SMO 名前空間の型を修飾します。

    Imports Microsoft.SqlServer.Management.Smo
    Imports Microsoft.SqlServer.Management.Common
    
  9. このプロシージャの後に続くコードをメイン プログラムに挿入します。

  10. アプリケーションを実行およびビルドします。

使用例

'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

参照

概念

キャプチャ モードの使用

ヘルプおよび情報

SQL Server 2005 の参考資料の入手