简体   繁体   English

SSIS在脚本任务上读取平面文件连接

[英]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. 我正在研究2008 SSIS,我需要读取一个平面文件,以便我可以访问其内容(有3个目录路径),因此我可以将这3个路径存储到变量中。

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. 平面文件将在3个不同的服务器中,根据我正在处理的实例(dev,qa,production),所以我不能只将路径写入变量,因为我每次都必须重写该值我需要在不同的实例中部署解决方案。

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). 我过去尝试过的是使用Directory.GetCurrentDirectory()读取平面文件,但我无法调试,并且在VS2008上使用F5 / run包不起作用(我读过它没有在VS上工作但是一旦你部署了它,它工作正常,但我没有办法证明它,但尝试)。

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. 所以,我发现,如果我可以读取保存在平面文件连接上的路径并将其保存在字符串变量中,我可以在部署包后修改.config文件中的连接字符串值,并读取其内容,如一个普通的平面文件。

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. 你想要一些像C#Script任务的东西。 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 . 在脚本中你可以修改(如果我Dts.Connections.["YourConnection"].ConnectionString话) 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: 要从脚本任务访问连接管理器信息,可以使用Dts.Connections属性,只需声明一个字符串变量,然后读取connectionstring属性:

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

Reference: 参考:

According to this Microsoft Docs article : 根据这篇Microsoft Docs文章

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. Script任务可以通过Dts对象的Connections属性访问这些连接管理器。 Each connection manager in the Connections collection stores information about how to connect to the underlying data source. Connections集合中的每个连接管理器都存储有关如何连接到基础数据源的信息。 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 生成了一个执行SQL任务
  • 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. 我按照这个帖子进行了参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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