共用方式為


以程式設計方式加入連接

ConnectionManager 類別代表外部資料來源的實體連接。ConnectionManager 類別會將連接的實作詳細資料與執行階段隔離。這可讓執行階段使用一致且可預測的方式與每個連接管理員互動。連接管理員包含一組所有連接共有的內建屬性,例如 NameIDDescription 以及 ConnectionString。不過,ConnectionStringName 屬性通常是唯一需要設定連接管理員的屬性。與其他程式設計範例不同的是,連接類別會公開像是 Open 或 Connect 等方法,以實體建立連至資料來源的連接,執行階段引擎則會在執行時管理封裝的所有連接。

Connections 類別是一種已經加入該封裝的連接管理員集合,而且可供在執行階段使用。您可以使用集合的 Add 方法,並提供指出連接管理員類型的字串,將更多的連接管理員加入集合。Add 方法會傳回加入封裝的 ConnectionManager 執行個體。

內建屬性

ConnectionManager 類別會公開一組所有連接都共有的屬性。然而,有時您需要存取特定連接類型特有的屬性。ConnectionManager 類別的 Properties 集合提供這些屬性的存取權。可以使用索引子或是屬性名稱以及 GetValue 方法,從集合擷取屬性,並且會使用 SetValue 方法來設定值。基礎連接物件屬性的屬性也可以透過取得物件的實際執行個體以及直接設定其屬性來設定。若要取得基礎連接,請使用連接管埋員的 InnerObject 屬性。下列程式碼行顯示以 C# 撰寫的程式碼,將建立具有基礎類別 ConnectionManagerAdoNetClass 的 ADO.NET 連接管理員。

ConnectionManagerAdoNetClass cmado = cm.InnerObject as ConnectionManagerAdoNet;

這會將 Managed 連接管理員物件轉換為它的基礎連接物件。如果您是使用 C++,會呼叫 ConnectionManager 物件的 QueryInterface 方法,而且會要求基礎連接物件的介面。

下表列出包含在 Integration Services 中的連接管理員。以及用在 package.Connections.Add("xxx") 陳述式中的字串。如需所有連接管理員的清單,請參閱<Integration Services 連接>。

字串

連接管理員

"OLEDB"

OLE DB 連接的連接管理員。

"ODBC"

ODBC 連接的連接管理員。

"ADO"

ADO 連接的連接管理員。

"ADO.NET:SQL"

ADO.NET (SQL 資料提供者) 連接的連接管理員。

"ADO.NET:OLEDB"

ADO.NET (OLE DB 資料提供者) 連接的連接管理員。

"FLATFILE"

一般檔案連接的連接管理員。

"FILE"

檔案連接的連接管理員。

"MULTIFLATFILE"

多個一般檔案連接的連接管理員。

"MULTIFILE"

多個一般檔案連接的連接管理員。

"SQLMOBILE"

SQL Server Compact 連接的連接管理員。

"MSOLAP100"

Analysis Services 連接的連接管理員。

"FTP"

FTP 連接的連接管理員。

"HTTP"

HTTP 連接的連接管理員。

"MSMQ"

Message Queuing (又稱為 MSMQ) 連接的連接管理員。

"SMTP"

SMTP 連接的連接管理員。

"WMI"

Microsoft Windows Management Instrumentation (WMI) 連接的連接管理員。

下列程式碼範例示範將 OLE DB 與 FILE 連接加入 PackageConnections 集合。範例接著會設定 ConnectionStringNameDescription 屬性。

using System;
using Microsoft.SqlServer.Dts.Runtime;

namespace Microsoft.SqlServer.Dts.Samples
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create a package, and retrieve its connections.
      Package pkg = new Package();
      Connections pkgConns = pkg.Connections;

      // Add an OLE DB connection to the package, using the 
      // method defined in the AddConnection class.
      CreateConnection myOLEDBConn = new CreateConnection();
      myOLEDBConn.CreateOLEDBConnection(pkg);

      // View the new connection in the package.
      Console.WriteLine("Connection description: {0}",
         pkg.Connections["SSIS Connection Manager for OLE DB"].Description);

      // Add a second connection to the package.
      CreateConnection myFileConn = new CreateConnection();
      myFileConn.CreateFileConnection(pkg);

      // View the second connection in the package.
      Console.WriteLine("Connection description: {0}",
        pkg.Connections["SSIS Connection Manager for Files"].Description);

      Console.WriteLine();
      Console.WriteLine("Number of connections in package: {0}", pkg.Connections.Count);

      Console.Read();
    }
  }
  // <summary>
  // This class contains the definitions for multiple
  // connection managers.
  // </summary>
  public class CreateConnection
  {
    // Private data.
    private ConnectionManager ConMgr;

    // Class definition for OLE DB Provider.
    public void CreateOLEDBConnection(Package p)
    {
      ConMgr = p.Connections.Add("OLEDB");
      ConMgr.ConnectionString = "Provider=SQLOLEDB.1;" +
        "Integrated Security=SSPI;Initial Catalog=AdventureWorks;" +
        "Data Source=(local);";
      ConMgr.Name = "SSIS Connection Manager for OLE DB";
      ConMgr.Description = "OLE DB connection to the AdventureWorks database.";
    }
    public void CreateFileConnection(Package p)
    {
      ConMgr = p.Connections.Add("File");
      ConMgr.ConnectionString = @"\\<yourserver>\<yourfolder>\books.xml";
      ConMgr.Name = "SSIS Connection Manager for Files";
      ConMgr.Description = "Flat File connection";
    }
  }

}
Imports Microsoft.SqlServer.Dts.Runtime

Module Module1

  Sub Main()

    ' Create a package, and retrieve its connections.
    Dim pkg As New Package()
    Dim pkgConns As Connections = pkg.Connections

    ' Add an OLE DB connection to the package, using the 
    ' method defined in the AddConnection class.
    Dim myOLEDBConn As New CreateConnection()
    myOLEDBConn.CreateOLEDBConnection(pkg)

    ' View the new connection in the package.
    Console.WriteLine("Connection description: {0}", _
      pkg.Connections("SSIS Connection Manager for OLE DB").Description)

    ' Add a second connection to the package.
    Dim myFileConn As New CreateConnection()
    myFileConn.CreateFileConnection(pkg)

    ' View the second connection in the package.
    Console.WriteLine("Connection description: {0}", _
      pkg.Connections("SSIS Connection Manager for Files").Description)

    Console.WriteLine()
    Console.WriteLine("Number of connections in package: {0}", pkg.Connections.Count)

    Console.Read()

  End Sub

End Module

' This class contains the definitions for multiple
' connection managers.

Public Class CreateConnection
  ' Private data.
  Private ConMgr As ConnectionManager

  ' Class definition for OLE DB provider.
  Public Sub CreateOLEDBConnection(ByVal p As Package)
    ConMgr = p.Connections.Add("OLEDB")
    ConMgr.ConnectionString = "Provider=SQLOLEDB.1;" & _
      "Integrated Security=SSPI;Initial Catalog=AdventureWorks;" & _
      "Data Source=(local);"
    ConMgr.Name = "SSIS Connection Manager for OLE DB"
    ConMgr.Description = "OLE DB connection to the AdventureWorks database."
  End Sub

  Public Sub CreateFileConnection(ByVal p As Package)
    ConMgr = p.Connections.Add("File")
    ConMgr.ConnectionString = "\\<yourserver>\<yourfolder>\books.xml"
    ConMgr.Name = "SSIS Connection Manager for Files"
    ConMgr.Description = "Flat File connection"
  End Sub

End Class

範例輸出:

連接描述:AdventureWorks 資料庫的 OLE DB 連接。

連接描述:AdventureWorks 資料庫的 OLE DB 連接。

封裝中的連接數目:2

Integration Services 圖示 (小) 掌握 Integration Services 的最新狀態

若要取得 Microsoft 的最新下載、文件、範例和影片以及社群中的選定解決方案,請瀏覽 MSDN 或 TechNet 上的 Integration Services 頁面:

若要得到這些更新的自動通知,請訂閱該頁面上所提供的 RSS 摘要。