Aggiunta di connessioni a livello di programmazioneAdding Connections Programmatically

La classe ConnectionManager rappresenta le connessioni fisiche alle origini dati esterne.The ConnectionManager class represents physical connections to external data sources. La classe ConnectionManager isola i dettagli di implementazione della connessione dal runtime.The ConnectionManager class isolates the implementation details of the connection from the runtime. In questo modo il runtime è in grado di interagire con ogni gestione connessione in modo coerente e stimabile.This enables the runtime to interact with each connection manager in a consistent and predictable manner. Le gestioni connessione contengono un set di proprietà predefinite comuni a tutte le connessioni, ad esempio Name, ID, Description e ConnectionString.Connection managers contain a set of stock properties that all connections have in common, such as the Name, ID, Description, and ConnectionString. Le proprietà ConnectionString e Name, tuttavia, sono in genere le uniche necessarie per configurare una gestione connessione.However, the ConnectionString and Name properties are ordinarily the only properties required to configure a connection manager. A differenza di altri paradigmi di programmazione, in cui le classi di connessione espongono metodi quali aprire o Connetti per stabilire fisicamente una connessione all'origine dati, il motore di run-time gestisce tutte le connessioni per il pacchetto mentre è in esecuzione.Unlike other programming paradigms, where connection classes expose methods such as Open or Connect to physically establish a connection to the data source, the run-time engine manages all the connections for the package while it runs.

La classe Connections è una raccolta delle gestioni connessione aggiunte a tale pacchetto e disponibili per essere utilizzate in fase di esecuzione.The Connections class is a collection of the connection managers that have been added to that package and are available for use at run time. È possibile aggiungere altre gestioni connessione alla raccolta utilizzando il metodo Add della raccolta e fornendo una stringa che indica il tipo di gestione connessione.You can add more connection managers to the collection by using the Add method of the collection, and supplying a string that indicates the connection manager type. Il metodo Add restituisce l'istanza ConnectionManager aggiunta al pacchetto.The Add method returns the ConnectionManager instance that was added to the package.

Proprietà intrinsecheIntrinsic Properties

La classe ConnectionManager espone un set di proprietà comuni a tutte le connessioni.The ConnectionManager class exposes a set of properties that are common to all connections. Tuttavia, è talvolta necessario accedere a proprietà univoche per il tipo di connessione specifico.However, sometimes you need access to properties that are unique to the specific connection type. La raccolta Properties della classe ConnectionManager fornisce l'accesso a queste proprietà.The Properties collection of the ConnectionManager class provides access to these properties. La proprietà può essere recuperata dalla raccolta utilizzando l'indicizzatore o il nome della proprietà e GetValue (metodo) e i valori vengono impostati utilizzando il SetValue metodo.The properties can be retrieved from the collection using the indexer or the property name and the GetValue method, and the values are set using the SetValue method. È possibile impostare le proprietà delle proprietà dell'oggetto connessione sottostante anche acquisendo un'istanza effettiva dell'oggetto e impostandone direttamente le proprietà.The properties of the underlying connection object properties can also be set by acquiring an actual instance of the object and setting its properties directly. Per ottenere la connessione sottostante, utilizzare la proprietà InnerObject della gestione connessione.To get the underlying connection, use the InnerObject property of the connection manager. Nella riga seguente di codice è mostrata una riga C# che crea una gestione connessione ADO.NET che dispone della classe sottostante, ConnectionManagerAdoNetClass.The following line of code shows a C# line that creates an ADO.NET connection manager that has the underlying class, ConnectionManagerAdoNetClass.

ConnectionManagerAdoNetClass cmado = cm.InnerObject as ConnectionManagerAdoNet;

Esegue il cast dell'oggetto gestione connessione gestito all'oggetto connessione sottostante.This casts the managed connection manager object to its underlying connection object. Se si utilizza C++, il QueryInterface metodo il ConnectionManager oggetto viene chiamato e viene richiesta l'interfaccia dell'oggetto connessione sottostante.If you are using C++, the QueryInterface method of the ConnectionManager object is called and the interface of the underlying connection object is requested.

Nella tabella seguente sono elencate le gestioni connessioni incluse in Integration ServicesIntegration Services.The following table lists the connection managers included with Integration ServicesIntegration Services. e la stringa utilizzata nell'istruzione package.Connections.Add("xxx")and the string that is used in the package.Connections.Add("xxx") statement. Per un elenco di tutte le gestioni connessioni, vedere Integration Services ( SSIS ) Connessioni.For a list of all connection managers, see Integration Services (SSIS) Connections.

StringString Gestione connessioneConnection manager
"OLEDB""OLEDB" Gestione connessione per le connessioni OLE DB.Connection manager for OLE DB connections.
"ODBC""ODBC" Gestione connessione per le connessioni ODBC.Connection manager for ODBC connections.
"ADO""ADO" Gestione connessione per le connessioni ADO.Connection manager for ADO connections.
"ADO.NET:SQL""ADO.NET:SQL" Gestione connessione per le connessioni ADO.NET (provider di dati SQL).Connection manager for ADO.NET (SQL data provider) connections.
"ADO.NET:OLEDB""ADO.NET:OLEDB" Gestione connessione per le connessioni ADO.NET (provider di dati OLE DB).Connection manager for ADO.NET (OLE DB data provider) connections.
"FLATFILE""FLATFILE" Gestione connessione per le connessioni file flat.Connection manager for flat file connections.
"FILE""FILE" Gestione connessione per le connessioni file.Connection manager for file connections.
"MULTIFLATFILE""MULTIFLATFILE" Gestione connessione per le connessioni di più file flat.Connection manager for multiple flat file connections.
"MULTIFILE""MULTIFILE" Gestione connessione per le connessioni di più file.Connection manager for multiple file connections.
"SQLMOBILE""SQLMOBILE" Gestione connessione per SQL ServerSQL Server Compact connessioni.Connection manager for SQL ServerSQL Server Compact connections.
"MSOLAP100""MSOLAP100" Gestione connessione per le connessioni Analysis ServicesAnalysis Services.Connection manager for Analysis ServicesAnalysis Services connections.
"FTP""FTP" Gestione connessione per le connessioni FTP.Connection manager for FTP connections.
"HTTP""HTTP" Gestione connessione per le connessioni HTTP.Connection manager for HTTP connections.
"MSMQ""MSMQ" Gestione connessione per le connessioni di accodamento messaggi (anche noto come MSMQ).Connection manager for Message Queuing (also known as MSMQ) connections.
"SMTP""SMTP" Gestione connessione per le connessioni SMTP.Connection manager for SMTP connections.
"WMI""WMI" Gestione connessione per le connessioni Strumentazione gestioneWindows (WMI) di Microsoft.Connection manager for Microsoft Windows Management Instrumentation (WMI) connections.

Nell'esempio di codice seguente è illustrata l'aggiunta di una connessione FILE e OLE DB alla raccolta Connections di un oggetto Package.The following code example demonstrates adding an OLE DB and FILE connection to the Connections collection of a Package. Nell'esempio vengono quindi impostate le proprietà ConnectionString, Name e Description.The example then sets the ConnectionString, Name, and Description properties.

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  

Esempio di Output:Sample Output:

Connection description: OLE DB connection to the AdventureWorks database.

Connection description: OLE DB connection to the AdventureWorks database.

Number of connections in package: 2

Risorse esterneExternal Resources

Articolo tecnico relativo le stringhe di connessione, sul sito Web carlprothman.net.Technical article, Connection Strings, on carlprothman.net.

Vedere ancheSee Also

Integration Services ( SSIS ) Connessioni Integration Services (SSIS) Connections
Creare gestioni connessioniCreate Connection Managers