次の手順と例は、オブジェクト構築を構成して、TestObjectConstruct クラスの既定の初期化文字列を "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes" 文字列に設定する方法を示します。この文字列は SQL Server データベースに接続するために使用されます。「オブジェクト構築」のトピックで、System.EnterpriseServices.ServicedComponent クラスからの COM+ オブジェクト構築の詳しい使用方法について説明しています。
オブジェクト構築を構成してクラスの既定の初期化文字列を設定するには
-
System.EnterpriseServices.ServicedComponent クラスから直接または間接的に派生したクラスを定義します。たとえば、次のコードは、System.EnterpriseServices.ServicedComponent クラスから直接派生した TestObjectConstruct クラスを示しています。
Imports System.EnterpriseServices
Imports System
Imports System.Data
Imports System.Data.SqlClient
…
Public Class TestObjectConstruct
Inherits ServicedComponent
…
End Class
using System;
using System.EnterpriseServices;
using System.Data;
using System.Data.SqlClient;
…
public class TestObjectConstruct : ServicedComponent
{
…
}
-
ConstructionEnabled 属性をクラスに適用して、属性のDefault プロパティを設定します。たとえば、次のコードでは、ConstructionEnabled 属性を TestObjectConstruct クラスに適用して、Default プロパティを SQL Server の接続文字列に設定しています。
<ConstructionEnabled([Default] := "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")> _
Public Class TestObjectConstruct
…
End Class
[ConstructionEnabled(Default="Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")]
public class TestObjectConstruct : ServicedComponent
{
…
}
-
Construct メソッドをオーバーライドします。
<ConstructionEnabled([Default] := "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")> _
Public Class TestObjectConstruct
Inherits ServicedComponent
Private m_connectStr As String
Private conn as SqlConnection
Protected Overrides Sub Construct(constructString As String)
' Called after constructor.
m_connectStr = constructString
End Sub
Public Sub ConnectToDatabase()
conn = New SqlConnection(m_connectStr)
End Sub
End Class
[C#]
[ConstructionEnabled(Default="Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes")]
public class TestObjectConstruct : ServicedComponent
{
private string connectStr;
SqlConnection conn;
public TestObjectConstruct()
{
…
}
protected override void Construct(string constructString)
{
// Called after constructor.
connectStr = constructString;
}
public void ConnectToDatabase()
{
conn = new SqlConnection(connectStr);
conn.Open();
}
}
-
クライアント アプリケーションでは、構築文字列を指定せずにコンポーネント クラスのインスタンスを作成して、既定値が使用されるようにします。たとえば、次のコードでは、TestObjectConstruct クラスのインスタンスが作成され、構築文字列は既定で "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes" になります。
Public Class App
Overloads Public Shared Sub Main()
Dim order As New TestObjectConstruct()
order.ConnectToDatabase()
End Sub
End Class
public class App
{
public static void Main()
{
TestObjectConstruct order = new TestObjectConstruct();
order. ConnectToDatabase();
}
}
コンポーネント サービス アプリケーションをインストールしたら、コンポーネント サービス管理ツールを使用して構築文字列を指定できます。コンポーネントのオブジェクト構築文字列を入力するには、次の手順に従ってください。
-
コンポーネント サービス管理ツールを起動します。
-
コンポーネント サービス管理ツールで、構成するコンポーネントを右クリックし、[プロパティ] をクリックします。
-
ConstructionEnabled 属性を True に設定していない場合は、[プロパティ] ダイアログ ボックスの [アクティブ化] タブで、[オブジェクトの構築を有効にする] チェック ボックスをオンにして、オブジェクト構築文字列の使用を有効にします。
-
構築文字列を ConstructionEnabled 属性で指定された既定から変更する場合は、[コンストラクタ文字列] ボックスに構築文字列を入力します。
使用例
Imports System.EnterpriseServices
Imports System
Imports System.Data
Imports System.Data.SqlClient
<assembly: ApplicationName("OCDemo")>
Namespace OCDemo
<ConstructionEnabled([Default] := "Initial Catalog=Northwind;
Data Source=.\\SQLServerInstance;Trusted_Connection=yes")> _
Public Class TestObjectConstruct
Inherits ServicedComponent
Private m_connectStr As String
Private conn as SqlConnection
Protected Overrides Sub Construct(constructString As String)
' Called after constructor.
m_connectStr = constructString
End Sub
Public Sub ConnectToDatabase()
conn = New SqlConnection(m_connectStr)
End Sub
End Class
End Namespace
using System;
using System.EnterpriseServices;
using System.Data;
using System.Data.SqlClient;
[assembly : ApplicationName("OCDemo")]
namespace OCDemo
{
[ConstructionEnabled(Default="Initial Catalog=Northwind;
Data Source=.\\SQLServerInstance;Trusted_Connection=yes")]
public class TestObjectConstruct : ServicedComponent
{
private string connectStr;
SqlConnection conn;
public TestObjectConstruct()
{
…
}
protected override void Construct(string constructString)
{
// Called after constructor.
connectStr = constructString;
}
public void ConnectToDatabase()
{
conn = new SqlConnection(connectStr);
conn.Open();
}
}
}
参照