Excel2 Custom Connection Manager Sample

New: 14 April 2006

The Excel2 Custom Connection Manager sample demonstrates how to create a custom connection manager, the Excel2 Connection Manager, and its associated user interface. This sample is not supported on Itanium-based operating systems.

For more information about how to create a custom connection manager, see Creating a Custom Connection Manager. For general information about the use of connection managers in Integration Services, see Integration Services Connections and Connection Managers.

The custom connection manager connects to an available Microsoft Excel workbook by using the OleDb .NET Framework Data Provider and the Microsoft OLE DB Provider for Jet, and returns an open OleDbConnection object from its AcquireConnection method. When you create a new instance of the Excel2 Connection Manager in a package, its custom user interface prompts you for the path of the Excel workbook. This connection manager also provides a check box to specify Import Mode. This resolves an issue that occurs frequently when an Excel column contains mixed text and numeric data.

Note

The Excel2 Custom Connection Manager sample cannot be used with the Excel source and destination. Test it with the DataReader source and destination instead.

This code sample implements the following functionality:

Important

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples.

Running the Sample

If you already know how to locate, build, and install code samples, you can go directly to the section Testing the Sample, and read about how to configure and run this sample.

Prerequisites

This sample requires that the following components are installed.

  • Microsoft Visual Studio 2005
  • Microsoft SQL Server 2005 Integration Services

Location

If the code sample was installed to the default location, the sample is located in the following directory:

C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services\\Programming Samples\Control Flow\Excel2ConnectionManager Sample

The C# solution for the code sample is located in the Excel2ConnMgrCS directory, and the Visual Basic solution is located in the Excel2ConnMgrVB directory.

For information about the two-step process required to install the samples, see Installing Samples. To obtain the latest version of the samples, including new samples released after the original release of SQL Server 2005, see SQL Server 2005 Samples and Sample Databases (April 2006).

Building the Sample

If you have not already generated a strong name key file in the Samples folder, use the following procedure to generate this key file. The sample projects are configured on the Signing tab of the Project Properties dialog box to sign assemblies at build time with this key file.

To generate a strong name key file

  1. To open a Microsoft Visual Studio 2005 command prompt, click Start, point to All Programs, point to Microsoft Visual Studio 2005, point to Visual Studio Tools, and then click Visual Studio 2005 Command Prompt.

    - or -

    To open a Microsoft .NET Framework command prompt, click Start, point to All Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. At the command prompt, use the change directory (CD) command to change the current folder of the Command Prompt window to the Samples folder. The key file that you create in this folder will be used by all SQL Server 2005 code samples.

    Note

    To determine the folder where samples are located, click Start, point to All Programs, point to Microsoft SQL Server 2005, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\90\Samples.

  3. At the command prompt, run the following command to generate the key file:

    sn -k SampleKey.snk
    

    Important

    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.

  4. You will need the public key token from the key file in a subsequent step. To obtain the public key token, first extract the public key from the key file to a new file by running the following command at the command prompt:

    sn -p SampleKey.snk SampleKeyPublic.snk
    

    Now display the public key token from the new file by running the following command at the command prompt:

    sn -t SampleKeyPublic.snk
    

    Copy the public key token to the clipboard or save it for later use.

To build the sample in Microsoft Visual Studio 2005

  1. From the File | Open menu, click Project and open the Excel2ConnMgrVB.sln or Excel2ConnMgrCS.sln.

  2. In the Solution Explorer, select the Excel2ConnMgrVB or Excel2ConnMgrCS project.

  3. Locate the DtsConnection attribute before the class declaration in the Excel2ConnMgrCS.cs file or the Excel2ConnMgrVB.vb file and replace the alphanumeric value of the Public Key Token in the UITypeName property of the attribute with the public key token obtained earlier from the key file.

  4. On the Build menu, click Build Solution to build the solution.

Installing the Sample

This sample is provided in both Visual Basic and C#. To distinguish the assemblies for each version of the sample, the name of the output assembly has CS or VB appended. After successfully building the component, follow these steps in order to add it as a connection manager in a package in Business Intelligence Development Studio.

To copy the component to the Connections folder

  1. Open Windows Explorer or your preferred application for working in the file system.

  2. Copy the assemblies (Excel2ConnMgrCS.dll and Excel2ConnMgrUICS.dll, or Excel2ConnMgrVB.dll and Excel2ConnMgrUIVB.dll) to the Connections folder located at %system%\Program Files\Microsoft SQL Server\90\DTS\Connections.

To install the component into the global assembly cache (GAC) by dragging and dropping the assemblies

  1. Open Windows Explorer or your preferred application for working in the file system.

  2. Drag the assemblies from the Connections folder to the folder where the global assembly cache (GAC) is located, at %system%\assembly.

To install the component into the global assembly cache (GAC) by using gacutil.exe

  1. Open a Command Prompt window.

  2. Type the following commands to install the C# version of the component and the user interface into the GAC:

    gacutil.exe -iF "C:\Program Files\Microsoft SQL Server\90\Samples\Integration Services\Programming Samples\Control Flow\Excel2ConnectionManager Sample\CS\Excel2ConnMgrCS\bin\Debug\Excel2ConnMgrCS.dll"
    gacutil.exe -iF "C:\Program Files\Microsoft SQL Server\90\Samples\Integration Services\Programming Samples\Control Flow\Excel2ConnectionManager Sample\CS\Excel2ConnMgrUICS\bin\Debug\Excel2ConnMgrUICS.dll"
    

    - or -

    Type the following commands to install the Visual Basic version of the component and the user interface into the GAC:

    gacutil.exe -iF " C:\Program Files\Microsoft SQL Server\90\Samples\Integration Services\Programming Samples\Control Flow\Excel2ConnectionManager Sample\VB\Excel2ConnMgrVB\bin\Debug\Excel2ConnMgrVB.dll"
    gacutil.exe -iF " C:\Program Files\Microsoft SQL Server\90\Samples\Integration Services\Programming Samples\Control Flow\Excel2ConnectionManager Sample\VB\Excel2ConnMgrVB\bin\Debug\Excel2ConnMgrUIVB.dll"
    

Testing the Sample

You can now test the custom connection manager in a package.

To use the sample connection manager in a package

  1. In the connection managers area of SSIS Designer, right-click, select New Connection, and in the list of available connection types, select EXCEL2. Click OK.

  2. Edit the new connection manager. In the Excel2 Connection Manager Editor, locate an available Excel workbook file. Use the check boxes to specify settings for the headers and Import Mode settings.

    Note

    The Excel2 Custom Connection Manager sample cannot be used with the Excel source, which supports only the built-in Excel connection manager. Test it with the DataReader source instead.