共用方式為


以程式設計方式加入連接

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

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

內建屬性

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

ConnectionManagerAdoNetClass cmado = cm.InnerObject as ConnectionManagerAdoNet;

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

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

String [ODBC 來源編輯器]
"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 連接加入 ConnectionsPackage 集合。 範例接著會設定 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  

範例輸出:

Connection description: OLE DB connection to the AdventureWorks database.

Connection description: OLE DB connection to the AdventureWorks database.

Number of connections in package: 2

外部資源

carlprothman.net 上的技術文章:連接字串

Integration Services 圖示 (小型) 使用 Integration Services 保持最新狀態
如需來自Microsoft的最新下載、文章、範例和影片,以及來自社群的所選解決方案,請流覽 MSDN 上的 Integration Services 頁面:

流覽 MSDN 上的 Integration Services 頁面

如需這些更新的自動通知,請訂閱頁面上可用的 RSS 摘要。

另請參閱

Integration Services (SSIS) 連接
建立連線管理員