1

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

我想要做的是使用我的 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();

关于如何将其与上传的文件结合起来的任何想法?

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

编辑(2):在Excel文件案例的问题解决之后,我想知道是否可以通过执行以下操作对平面文件执行相同的操作:

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


result = pckg.Execute();
4

2 回答 2

2

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

SSIS:

  1. 打开你的 SSIS 包
  2. 创建一个名为 Excel“FilePath”的包级变量。现在,有一些示例文件位置。
  3. 单击 Excel 连接管理器。转到属性并转到表达式并配置它,如屏幕截图所示。

在此处输入图像描述

控制台项目:

打开您的控制台项目并尝试以下代码。您应该从字符串 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();

希望这可以帮助!

于 2013-05-07T04:55:55.117 回答
1

If I understand your problem correctly (need to dynamically set the excel file name at runtime) then you'd just need to edit your connection string before executing. Code approximate

//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();
于 2013-05-06T18:20:56.833 回答