2013-05-06 115 views
1

我正在使用ASP.NET MVC 4和实体框架。我创建了一个SSIS包,它从Excel文件中提取数据并将其存储到数据库的表中。带有文件的SSIS包通过ActionResult

我想要做的就是使用我的SSIS包与上传的Excel文件(到ActionResult)来存储数据。

这里我有一个代码示例,它返回我“成功”。所以包正确执行:

Console.WriteLine("Loading SSIS Service..."); 
//Application object allows load your SSIS package 
Application app = new Application(); 
//In order to retrieve the status (success or failure) after running SSIS Package 
DTSExecResult result; 
//Specify the location of SSIS package - dtsx file 
string SSISPackagePath = @"C:\Package.dtsx"; 
//Load your package 
Package pckg = (Package)app.LoadPackage(SSISPackagePath, null); 
//Execute the package and retrieve result 
result = pckg.Execute(); 
//Print the status success or failure of your package 
Console.WriteLine("{0}", result.ToString()); 

Console.ReadLine(); 

有关如何将其与上传的文件耦合的任何想法?

编辑:好的包装工作正常,我只需要修改源文件。任何想法?

EDIT(2):在此之后,问题已经解决了Excel文件的情况下,我想知道是否有可能做这做同样的事情了平面文件

pckg.Connections["NameOfTheConnectionManager"].ConnectionString = @"C:\Test-CSV.csv"; 


result = pckg.Execute(); 
+0

最好就第二次编辑提出一个新问题。 – 2014-08-29 18:29:20

回答

1

如果我正确理解你的问题(需要在运行时动态设置excel文件名),那么你只需要在执行之前编辑你的连接字符串。代码近似

//Load your package 
Package pckg = (Package)app.LoadPackage(SSISPackagePath, null); 
// This needs to correspond to the CM's name in the package 
// and the properties of the current CM's ConnectionString 
pckg.Connections["Excel Connection Manager"].ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\fileName.xls;Extended Properties=""EXCEL 8.0;HDR=YES"";"; 
//Execute the package and retrieve result 
result = pckg.Execute(); 
+0

谢谢,它帮助我!但是,我还有一个问题:如何为平面文件做同样的事情?假设没有用于这种文件的“Excel连接管理器”,我不知道如何继续... – Traffy 2013-05-07 14:39:31

+0

假设平面文件的连接管理器不存在(因此需要创建)或者您是否说CM为平面文件存在,你需要做同样的动态位置逻辑的应用? – billinkc 2013-05-07 15:19:37

+0

第二种情况:CM存在,我需要执行相同的动态位置逻辑。 – Traffy 2013-05-08 11:38:03

2

我想你已经创建了控制台应用程序来运行你的包。您可以执行以下操作。

SSIS:

  1. 打开SSIS包
  2. 创建一个名为Excel的 “文件路径” 一包级别的变量。目前, 有一些示例文件位置。
  3. 单击Excel连接管理器。转到属性并转到 表达式并按照屏幕截图中所示进行配置。

enter image description here

控制台项目:

打开控制台项目,并尝试下面的代码。您应该从字符串arg []中路径文件路径。

Console.WriteLine("Loading SSIS Service..."); 
//Get the file path 
string filePath = args[0]; 
//Application object allows load your SSIS package 
Application app = new Application(); 
//In order to retrieve the status (success or failure) after running SSIS Package 
DTSExecResult result; 
//Specify the location of SSIS package - dtsx file 
string SSISPackagePath = @"C:\Package.dtsx"; 
//Load your package 
Package pckg = (Package)app.LoadPackage(SSISPackagePath, null); 
//Assign the source file path. File path from the argument[0]. 
pkg.Variables["FilePath"].Value = filePath; 
//Execute the package and retrieve result 
result = pckg.Execute(); 
//Print the status success or failure of your package 
Console.WriteLine("{0}", result.ToString()); 
Console.ReadLine(); 

希望这会有所帮助!

+0

谢谢,我会尝试一下,让它知道它是否正常工作! – Traffy 2013-05-07 09:00:43

+0

我想你的答案会对我的第二个案例有所帮助......另一个软件包,相同的概念,而不是使用Excel文件作为源文件,我使用的是平面文件(实际上是一个CSV文件)。这个可变的技巧会为此工作吗? – Traffy 2013-05-07 13:56:53

+0

同样的解决方案也适用于CSV文件。在表达式中,您应该使用连接字符串属性而不是ExcelFilePath。 – Gowdhaman008 2013-05-08 05:05:08