Solved

AcquireConnection method call generates 0xC020801C error

Posted on 2013-12-18
1
1,077 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
1 Comment
 
LVL 39

Accepted Solution

by:
lcohan earned 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Restore Procedure question 4 31
Sort by Month and Year - SQL 3 23
Database connection opened on a machine 8 37
Join vs where 2 11
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Over the last 2 years, I have been working on SSIS 2008. Really the tough tasks in SSIS are to deploy packages and pass parameters (Values from outside package). The latter is certainly a headache for developers, particularly for me. We had to ma…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now