このセクションでは、Visual Basic で SMO 例外をキャッチする方法について説明します。
コード例では、Try…Catch…Finally Visual Basic .NET ステートメントを使用して SMO 例外をキャッチする方法を示しています。SMO 例外はすべて SmoException 型であり、これらは SMO のリファレンスに一覧されています。エラーの原因を示すために、内部例外のシーケンスが表示されます。詳細については、Visual Basic .NET のドキュメントを参照してください。
プロシージャ タイトル
Visual Studio 2005 を起動します。
[ファイル] メニューの [新規作成] をポイントして [プロジェクト] をクリックします。[新しいプロジェクト] ダイアログ ボックスが表示されます。
[プロジェクトの種類] ペインで、[Visual Basic] をクリックします。[テンプレート] ペインで、[コンソール アプリケーション] をクリックします。
(省略可) [名前] ボックスに新しいアプリケーションの名前を入力します。
[OK] をクリックすると、Visual Basic コンソール アプリケーション テンプレートが読み込まれます。
[プロジェクト] メニューの [参照の追加] をクリックします。[参照の追加] ダイアログ ボックスが表示されます。[参照] をクリックして、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
[表示] メニューの [コード] をクリックします。または、[Module1.vb] ウィンドウをクリックしてコード ウィンドウを表示します。
コードでは、宣言の前に、次の Imports ステートメントを入力し、SMO 名前空間の型を修飾します。
Imports Microsoft.SqlServer.Management.Smo Imports Microsoft.SqlServer.Management.Commonこのプロシージャの後に続くコードをメイン プログラムに挿入します。
アプリケーションを実行およびビルドします。
使用例
'This sample requires the Microsoft.SqlServer.Management.Smo.Agent namespace is included.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define an Operator object variable by supplying the parent SQL Agent and the name arguments in the constructor.
'Note that the Operator type requires [] parenthesis to differentiate it from a Visual Basic key word.
Dim op As [Operator]
op = New [Operator](srv.JobServer, "Test_Operator")
op.Create()
'Start exception handling.
Try
'Create the operator again to cause an SMO exception.
Dim opx As OperatorCategory
opx = New OperatorCategory(srv.JobServer, "Test_Operator")
opx.Create()
'Catch the SMO exception
Catch smoex As SmoException
Console.WriteLine("This is an SMO Exception")
'Display the SMO exception message.
Console.WriteLine(smoex.Message)
'Display the sequence of non-SMO exceptions that caused the SMO exception.
Dim ex As Exception
ex = smoex.InnerException
Do While ex.InnerException IsNot (Nothing)
Console.WriteLine(ex.InnerException.Message)
ex = ex.InnerException
Loop
'Catch other non-SMO exceptions.
Catch ex As Exception
Console.WriteLine("This is not an SMO exception.")
End Try