ConnectionManager.ConnectionString Property

Gets or sets the connection string used to establish a connection to a data source.

Namespace:  Microsoft.SqlServer.Dts.Runtime
Assembly:  Microsoft.SqlServer.ManagedDTS (in Microsoft.SqlServer.ManagedDTS.dll)

public string ConnectionString { get; set; }

Property Value

Type: System.String
A String that contains the connection string value.

The format and contents of the connection string depend on the data provider, the data source, and the type of connection manager that is being created. To view a sample connection string, in BI Development Studio, create a package, and then create a connection manager of the appropriate type within that package. Next, in the Properties window, examine the ConnectionString property of the connection manager that you just created.

The following code example adds an OLE DB connection manager, and then sets the connection manager name and connection string properties.

// Add the OLE DB connection manager.
ConnectionManager AdventureWorks2008R2 = package.Connections.Add("OLEDB");
// Set stock properties.
AdventureWorks2008R2.Name = "OLEDBConnection";
AdventureWorks2008R2.ConnectionString = @"Provider=SQLNCLI10;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=AdventureWorks2008R2;Data Source=(local);Auto Translate=False;";

For an XML connection string, the connection string value is formatted similar to the following:

String myConnString = @"C:\Program Files\Microsoft SQL Server\orders.xml";

Whereas for an OLE DB connection, the connection string contains values similar to the following connection string:

String myConnString = "Server=MYSERVER;Provider=SQLOLEDB.1;Pwd= xxxxxxx;User ID= xxxxx;Initial Catalog=mySource;OLE DB Services=-2";

And an ODBC connection string may look similar to the following:

String myConnString = "DSN=LocalServer;DATABASE=MySource;PWD=xxxxxxx;UID=xxxxx";

In the properties dialog box of a connection in the SSIS Designer, this value appears as the Connection String property.

For a list of all connection managers included with the product, see Connection Managers.

The following code example creates a FileSystemTask as an Executable in a package. The FileSystemTask copies a test folder that contains two subfolders and a .txt file, to another folder. The source and destinations are existing folders. Therefore, the IsDestinationPathVariable and IsSourcePathVariable are set to false, and the Destination and Source properties ares set to reference two FILE connection managers that have been created and have had their connection strings set to reference Strings containing the paths.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.FileSystemTask;

namespace FileSystemTask_API
    class Program
        static void Main(string[] args)
            String sourceDir = @"C:\TestFolder";
            String destDir = @"C:\MyNewTestFolder";

            Package pkg = new Package();
            Executable exec1 = pkg.Executables.Add("STOCK:FileSystemTask");
            TaskHost th = exec1 as TaskHost;
            // Show how to set properties using the TaskHost
             // Properties. Set the properties to copy an existing
             // folder, which contains two subfolders and a .txt file,
            // to another existing folder on the C:\ drive.

            // The source or destination files are not in a variable,
            // so set IsSourcePathVariable and 
            // IsDestinationPathVariable to false.
            th.Properties["IsSourcePathVariable"].SetValue(th, false);
            th.Properties["IsDestinationPathVariable"].SetValue(th, false);

            // Create the File connection manager for the source.
            ConnectionManager cm = pkg.Connections.Add("FILE");
            cm.Name = "The FILE connection manager";
            cm.ConnectionString = sourceDir;
            cm.Properties["FileUsageType"].SetValue(cm, DTSFileConnectionUsageType.FolderExists);

            // Create the File connection manager for the destination.
            ConnectionManager cmdest = pkg.Connections.Add("FILE");
            cmdest.Name = "The destination FILE connection manager";
            cmdest.ConnectionString = destDir;
            cmdest.Properties["FileUsageType"].SetValue(cmdest, DTSFileConnectionUsageType.FolderExists);

            // Set the source property and destination properties
            // for the task.
            th.Properties["Source"].SetValue(th, cm.Name);
            th.Properties["Destination"].SetValue(th, cmdest.Name);

            // The operation to perform is to copy all the files and
             // subfolders in a folder.
            // Do not overwrite the destination information 
            // if it exists.
            th.Properties["Operation"].SetValue(th, DTSFileSystemOperation.CopyDirectory);
            th.Properties["OperationName"].SetValue(th, "Copy TestFolder");
            th.Properties["OverwriteDestinationFile"].SetValue(th, false);

            // Set the attribute of the folder to be read-only.
            th.Properties["Attributes"].SetValue(th, DTSFileSystemAttributes.ReadOnly);
            // Multiple attributes can be set. The next line of code,
            // commented out, shows how to do that.
            //th.Properties["Attributes"].SetValue(th, DTSFileSystemAttributes.ReadOnly | DTSFileSystemAttributes.Hidden);

            // Run the task and copy the folder.
            DTSExecResult result = pkg.Execute();

Community Additions