AcquireConnection method call generates 0xC020801C error


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?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

lcohanConnect With a Mentor Database AnalystCommented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.