vb.net 在 SSIS 的脚本任务中使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13407840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Variables in Script Task in SSIS
提问by user1757018
I am using a SSIS package with an Execute SQL Task connected to Script Task. The values from Execute SQL Task are stored in a variable and in the Script Task I use the following code to give XML file.
我正在使用 SSIS 包和连接到脚本任务的执行 SQL 任务。执行 SQL 任务的值存储在一个变量中,在脚本任务中,我使用以下代码提供 XML 文件。
Public Sub Main()
If My.Computer.FileSystem.FileExists("C:\SAMPLE.xml") Then
Dts.TaskResult = ScriptResults.Failure
Else
My.Computer.FileSystem.WriteAllText("C:\SAMPLE.xml", Dts.Variables(0).Value.ToString, False)
Dts.TaskResult = ScriptResults.Success
''
End If
End Sub
I don't want to hardcode the path to XML. So I created two new variables FileName and FilePath with package as scope. How do I edit my VB.Net code to actually use these two variables.I tried this but did not work:
我不想硬编码 XML 的路径。所以我创建了两个新变量 FileName 和 FilePath ,以包为范围。如何编辑我的 VB.Net 代码以实际使用这两个变量。我试过这个但没有用:
Sub Main()
If My.Computer.FileSystem.FileExists(Dts.Variables("FileDest").Value.ToString()) Else
My.Computer.FileSystem.WriteAllText(Dts.Variables("FileDest").Value.ToString(), Dts.Variables(0).Value.ToString, False)Dts.TaskResult = ScriptResults.Success
''
End If
End Sub
Ideally I want to use two variables one for name and one for path but when I tried with a single variable which combines both, it didn't work.
理想情况下,我想使用两个变量,一个用于名称,一个用于路径,但是当我尝试使用结合了两者的单个变量时,它不起作用。
回答by Edmund Schweppe
I suspect that part of your problem is using a numeric index into the Variablescollection. Once you added the FileDestvariable, there's no guarantee that whatever variable was being used to store the Execute SQL Task results is still the zeroeth one in the collection.
我怀疑您的问题的一部分是在Variables集合中使用数字索引。添加FileDest变量后,无法保证用于存储执行 SQL 任务结果的任何变量仍然是集合中的第零个。
Assuming the variable with the Execute SQL Task results is named XmlQueryResults, the following should do the trick:
假设具有 Execute SQL Task 结果的变量被命名为XmlQueryResults,以下应该可以解决问题:
Public Sub Main()
Dim filePath As String = Dts.Variables("FileDest").Value
Dim xmlToWrite As String = Dts.Variables("XmlQueryResults").Value
If My.Computer.FileSystem.FileExists(filePath) Then
Dts.TaskResult = ScriptResults.Failure
Else
My.Computer.FileSystem.WriteAllText(filePath, xmlToWrite, False)
Dts.TaskResult = ScriptResults.Success
End If
End Sub
(Don't forget to add the FileDestvariable to the ReadOnlyVariables in the Script Task Editor script tab.)
(不要忘记将FileDest变量添加到脚本任务编辑器脚本选项卡中的 ReadOnlyVariables。)

