Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AcquireConnection method call generates 0xC020801C error

Posted on 2013-12-18
1
Medium Priority
?
1,192 Views
Last Modified: 2016-02-10
Hi,

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);
            package.Executables.Add("STOCK:PipelineTask");
 
            // 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();
            instanceSource.ProvideComponentProperties();
 
            // 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);
            //instanceSource.AcquireConnections(null);
            //Dts.Events.FireInformation(0, "", "11a.) Calling ReinitializeMetaData().", "", 0, ref fireAgain);
            //instanceSource.ReinitializeMetaData();
            //instanceSource.ReleaseConnections();
            //// *********************************************************************************************************

            try
            {
                Dts.Events.FireInformation(0, "", "11.) Reinitialize the metadata, refresh columns.", "", 0, ref fireAgain);
                instanceSource.AcquireConnections(null);
                instanceSource.ReinitializeMetaData();
                instanceSource.ReleaseConnections();
            }
            catch (Exception ex)
            {
                Dts.Events.FireInformation(0, "", "11.) Encountered erros.  See Messageboxes.", "", 0, ref fireAgain);
                MessageBox.Show(ex.Message);
                if (ex.InnerException != null)
                    MessageBox.Show(ex.InnerException.Message);

                Dts.TaskResult = (int)ScriptResults.Failure;

                return;
            }
 

Open in new window



when the code hits the

instanceSource.AcquireConnections(null);

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

thanks
0
Comment
Question by:yechan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 40

Accepted Solution

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

http://www.codeproject.com/Articles/534651/HowplustoplussolveplusSSISpluserrorpluscodeplus0xC


"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

http://1.bp.blogspot.com/-WHsiqaOI8gA/T8_GuvdMMZI/AAAAAAAAAPE/SoqxcClaSvI/s1600/ssis.jpg


"
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

722 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