如何在 C# 中更改 ssis 中的连接属性
本文关键字:ssis 连接 属性 | 更新日期: 2023-09-27 18:30:31
我有这段代码来执行一个包。该包的作用只是将非常大的表数据导出到 excel 文件中。
[HttpPost]
public ActionResult TableToFile()
{
try
{
var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SSIS"].ConnectionString);
var integrationServices = new IntegrationServices(connection);
var package = integrationServices
.Catalogs["SSISDB"]
.Folders["MyFolder"]
.Projects["MyProject"]
.Packages["MyPackage.dtsx"];
long executionIdentifier = package.Execute(true, null);
ExecutionOperation eo = integrationServices.Catalogs["SSISDB"].Executions[executionIdentifier];
while (!eo.Completed)
{
eo.Refresh();
System.Threading.Thread.Sleep(5000);
}
return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);
}
catch (Exception e)
{
return Json(new { result = "error" }, JsonRequestBehavior.AllowGet);
}
}
但是,该包具有硬编码连接,如 sql 服务器源和 excel 文件目标文件名。我们可以在包的连接管理器中看到。
我在 VB.Net 有这段代码,并使用不同的库也设置了包的连接
Dim source As String = System.Windows.Forms.Application.StartupPath() & "'export.dtsx"
Dim p As Microsoft.SqlServer.Dts.Runtime.Package = app.LoadPackage(source, Nothing)
Dim exec As Executables = p.Executables
For Each config As ConnectionManager In p.Connections
Select Case config.Name
Case "SourceConnectionOLEDB"
config.ConnectionString = "..... some connection string here"
Case "DestinationConnectionFlatFile"
config.ConnectionString = file
Dim inner As Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerFlatFile90 = CType(config.InnerObject, Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerFlatFile90)
For i As Integer = 0 To inner.Columns.Count - 1
Dim innerColumn As Microsoft.SqlServer.Dts.Runtime.Wrapper.IDTSConnectionManagerFlatFileColumn90 = inner.Columns(i)
If i < inner.Columns.Count - 1 Then
innerColumn.ColumnDelimiter = delimiter
Else
innerColumn.ColumnDelimiter = vbNewLine
End If
Next
End Select
Next
For Each taskhost As TaskHost In exec
taskhost.Execute(Nothing, Nothing, Nothing, Nothing, Nothing)
Next
我的问题是,在我像 VB.Net 一样在上面的 C# 代码中执行包之前,如何更改连接字符串或某些设置?
由于您似乎正在使用 SSIS 目录,因此您还应该能够使用项目环境变量。基本上,您的包将引用这些变量,并且您将在执行之前设置值。
对此有解释 http://www.sqlchick.com/entries/2015/1/4/parameterizing-connections-and-values-at-runtime-using-ssis-environment-variables