共用方式為


HOW TO:設定物件建構

以下程序和範例描述如何設定物件建構,以及將 TestObjectConstruct 類別的預設初始化字串設定為字串 "Initial Catalog=Northwind;Data Source=.\\SQLServerInstance;Trusted_Connection=yes"。這個字串是用來連接到 SQL Server 資料庫的字串。物件建構主題進一步說明如何從 ServicedComponent 類別使用 COM+ 物件建構。

設定物件建構和設定類別的預設初始化字串

  1. 定義直接或間接衍生自 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
    {
       …
    }
    
  2. ConstructionEnabledAttribute 屬性套用到此類別,並設定屬性 (Attribute) 的 Default 屬性 (Property)。例如,以下程式碼將 ConstructionEnabledAttribute 屬性套用到 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
    {
        …
    }
    
  3. 覆寫 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();
         }
    }
    
  4. 在用戶端應用程式中,建立元件類別的執行個體,但不指定建構字串,以使用預設值。例如,以下程式碼會建立 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();
          }
    }
    

一旦安裝了元件服務應用程式,即可使用「元件服務」系統管理工具來指定建構字串。若要輸入元件的物件建構字串,請執行以下步驟:

  1. 開啟「元件服務」系統管理工具。

  2. 在「元件服務」系統管理工具中,在您想設定的元件上按一下滑鼠右鍵,然後按一下 [內容]。

  3. 如果您尚未將 ConstructionEnabled 屬性設為 True,請在 [內容] 對話方塊的 [啟動] 索引標籤上,選取 [啟用物件建構] 核取方塊,以啟用物件建構字串的使用。

  4. 若要變更以 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();
     }
    }
}

請參閱

參考

ConstructionEnabledAttribute
System.EnterpriseServices

概念

物件建構
可用 COM+ 服務摘要

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.