我正在使用ASP.NET MVC 4和Entity Framework。我创建了一个SSIS包,它从Excel文件中提取数据并将其存储在我的数据库中的表中。
我想要做的是将我的SSIS包与上传的Excel文件(ActionResult
)一起使用来存储数据。
这里我有一个代码示例,它返回“Success”。所以包正确执行:
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文件的情况,我想通过这样做知道是否可以对平面文件做同样的事情:< / p>
pckg.Connections["NameOfTheConnectionManager"].ConnectionString = @"C:\Test-CSV.csv";
result = pckg.Execute();
答案 0 :(得分:2)
我认为你已经创建了控制台应用程序来运行你的包。您可以执行以下操作。
<强> SSIS:强>
控制台项目:
打开控制台项目并尝试以下代码。您应该从字符串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();
希望这有帮助!
答案 1 :(得分: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();