AcquireConnection method call generates 0xC020801C error

Posted on 2013-12-18
Last Modified: 2016-02-10

I have a script task that is hopefully going to create a Data Flow Task during runtime.  Below is what I have so far.

bool fireAgain = false;
            Package package = new Package();
            package.Name = "SqlToFlatFile";
            //Get variable from for each loop container
            string ExecuteSQL;
            string SourceConnectionString;
            string FileName;

            Dts.Events.FireInformation(0, "", "1.) Setting variables.", "", 0, ref fireAgain);
            ExecuteSQL = "exec dbo.SayHello '12/18/2013'";  //(Dts.Variables["ExecuteSQL"].Value).ToString();
            SourceConnectionString = @"Server=localhost; Database=someDatabase;User Id=someUser; Password=somePassword";   //(Dts.Variables["SourceConnectionString"].Value).ToString();
            FileName = @"C:\TEST.CSV";  //(Dts.Variables["FileName"].Value).ToString();

            Dts.Events.FireInformation(0, "", "2.) Add the SQL OLE_DB Connection.", "", 0, ref fireAgain);
            // Add the SQL OLE-DB connection
            ConnectionManager connectionManagerOleDb = package.Connections.Add("OLEDB");
            connectionManagerOleDb.Name = "OLEDB";
            connectionManagerOleDb.ConnectionString = SourceConnectionString;  
            connectionManagerOleDb.Properties["RetainSameConnection"].SetValue(connectionManagerOleDb, true);

            Dts.Events.FireInformation(0, "", "3.) Add the Flat File DB connection, basic info only, will define add columns later.", "", 0, ref fireAgain);
            // Add the Flat File DB connection, basic info only, will define add columns later
            ConnectionManager connectionManagerFlatFile = package.Connections.Add("FLATFILE");
            connectionManagerFlatFile.ConnectionString = FileName;            connectionManagerFlatFile.Name = "FlatFile";
            connectionManagerFlatFile.Properties["Format"].SetValue(connectionManagerFlatFile, "Delimited");
            connectionManagerFlatFile.Properties["ColumnNamesInFirstDataRow"].SetValue(connectionManagerFlatFile, true);
            //Set the connection string of the flat file destination component to an expression
            Dts.Events.FireInformation(0, "", "4.) Set the connection string of the flat file destination component to an expression.", "", 0, ref fireAgain);
            string expression = FileName;            connectionManagerFlatFile.Properties["ConnectionString"].SetExpression(connectionManagerFlatFile, expression);
            // Add the Data Flow Task
            Dts.Events.FireInformation(0, "", "5.) Add the Data Flow Task", "", 0, ref fireAgain);
            // Get the task host wrapper, and the Data Flow task
            Dts.Events.FireInformation(0, "", "6.) Get the task host wrapper, and the Data Flow task", "", 0, ref fireAgain);
            TaskHost taskHost = package.Executables[0] as TaskHost;
            MainPipe dataFlowTask = (MainPipe)taskHost.InnerObject;
            // Add OLE-DB source component
            Dts.Events.FireInformation(0, "", "7.) Add OLE-DB source component", "", 0, ref fireAgain);
            IDTSComponentMetaData100 componentSource = dataFlowTask.ComponentMetaDataCollection.New();
            componentSource.Name = "OLEDBSource";
            componentSource.ComponentClassID = "DTSAdapter.OleDbSource.2";
            // Get OLE-DB source design-time instance, and initialise component
            Dts.Events.FireInformation(0, "", "8.) Get OLE-DB source design-time instance, and initialise component.", "", 0, ref fireAgain);
            CManagedComponentWrapper instanceSource = componentSource.Instantiate();
            // Set source connection
            Dts.Events.FireInformation(0, "", "9.) Set source connection.", "", 0, ref fireAgain);
            componentSource.RuntimeConnectionCollection[0].ConnectionManagerID = connectionManagerOleDb.ID;
            componentSource.RuntimeConnectionCollection[0].ConnectionManager = DtsConvert.GetExtendedInterface(connectionManagerOleDb);
            //decalre dynamic sql
            //string mySQLText = "Exec" + " " + SPNames + " " + RunDate;
            // Set the source properties
            Dts.Events.FireInformation(0, "", "10.) Set the source properties.", "", 0, ref fireAgain);
            instanceSource.SetComponentProperty("AccessMode", 2);
            instanceSource.SetComponentProperty("SqlCommand", ExecuteSQL);  
            //// *********************************************************************************************************
            //// Reinitialize the metadata, refresh columns
            //Dts.Events.FireInformation(0, "", "11.) Reinitialize the metadata, refresh columns.", "", 0, ref fireAgain);
            //Dts.Events.FireInformation(0, "", "11a.) Calling ReinitializeMetaData().", "", 0, ref fireAgain);
            //// *********************************************************************************************************

                Dts.Events.FireInformation(0, "", "11.) Reinitialize the metadata, refresh columns.", "", 0, ref fireAgain);
            catch (Exception ex)
                Dts.Events.FireInformation(0, "", "11.) Encountered erros.  See Messageboxes.", "", 0, ref fireAgain);
                if (ex.InnerException != null)

                Dts.TaskResult = (int)ScriptResults.Failure;


Open in new window

when the code hits the


line, it blows up.  Anyone here able to help with this error?

Question by:yechan
1 Comment
LVL 39

Accepted Solution

lcohan earned 500 total points
ID: 39727895
Did you looked at the details mentioned in this article?

"Most of the problems of running well in BIDS but not on SQL job arise due to these situations:

Data source connection or File access issue (when the user running the sql agent doesn't have the right permissions to the db or to the destination file).

Package protection level (pwd are sensitive data, and sometimes are not copied depending on the protection level).

64bit issue (Since there's no Jet Driver for 64bit systems, when running on a 64bit OS you need to either use the 32bit DTEXEC in your sql job or set the "Use 32bit runtime" option, it's on the "Execution options" tab when you're creating a SSIS job step, check the image on link below for seeing it).

32Bit runtime option image


Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 48
SSRS 2013 - Creating a summarized report 19 35
sql server service accounts 4 23
abs operation in sql server 15 11
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question