简体   繁体   中英

SSIS read flat file connection on script task

I'm working on a 2008 SSIS in which I need to read a flat file so that I can access its content (which has 3 directories paths), so I can store those 3 paths into variables.

The flat file would be in 3 different servers, according to the instance I'm working on (dev,qa,production), so I can't just write the path into a variable because I'd have to rewrite that value every time I'd need to deploy the solution in a different instance.

Something I've tried on the past is to read a flat file using the Directory.GetCurrentDirectory(), but I couldn't debug that and using the F5/run package on VS2008 didn't work (I've read that it doesn't work on VS but once you deploy the package it works fine, but I have no means to prove it but to try).

So, I figured out that, If I can read the path saved on a flat file connection and save it in a string variable, I could modify the connection string value in the .config file once the package is deployed, and read its contents like a normal flat file.

My problem is that I can't figure out how to read the connection string value, and I couldn't find anything online that pointed me in the right direction.

Thanks in advance.

You'd want something like a C# Script task. You can modify the connection string dynamically there. Within the script you'd modify the value of (if I recall correctly) Dts.Connections.["YourConnection"].ConnectionString .

To access connection managers informations from script tasks you can use Dts.Connections property, just declare a string variable, and read the connectionstring property:

string cs;
cs = Dts.Connections["myFlatFileConnection"].AcquireConnection(Dts.Transaction);

Reference:

According to this Microsoft Docs article :

Connection managers provide access to data sources that have been configured in the package. For more information. The Script task can access these connection managers through the Connections property of the Dts object. Each connection manager in the Connections collection stores information about how to connect to the underlying data source. Read more (+examples)

Since nothing seemed to work, I ended up doing the following:

  • Inserted the values I needed in a parameters table in the database
  • generated an Execute SQL Task
  • assigned the results of that task to the variables

It took me all day but I finally got it.

I followed this thread for references.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM