2015-03-03 82 views
0

我已经问过这个问题了,我想我找到了解决方案,但我再次陷入困境。我遇到了解锁变量的问题。SSIS中的解锁变量

我正在运行一个SQL Server代理作业,它具有下面的Microsoft Visual C#2008脚本任务。我没有列出脚本任务编辑器读/写变量列表中的变量。在我的SSIS包中,我有For循环容器,循环这个脚本任务直到文件存在。

https://mitchellsql.wordpress.com/2014/09/06/ssis-script-taskdoes-file-exists/

public void Main() 
    { 

     // Lock variables 
     Dts.VariableDispenser.LockForRead("User::FolderPath"); 
     Dts.VariableDispenser.LockForWrite("User::FileExistsFlg"); 

     // Create a variables 'container' to store variables 
     Variables vars = null; 

     // Add variables from the VariableDispenser to the variables 'container' 
     Dts.VariableDispenser.GetVariables(ref vars); 

     string filepath; 
     filepath = vars["User::FolderPath"].Value.ToString(); 
     vars["User::FileExistsFlg"].Value = File.Exists(filepath); 

     // Release the locks 
     vars.Unlock(); 

     Dts.TaskResult = (int)ScriptResults.Success;  
    } 
    } 
} 

错误

我得到这个错误时,SQL作业失败,但包完美运行时通过Visual Studio中运行:

消息

Executed as user: PSFACAMDWHSQL1\SYSTEM. Microsoft (R) SQL Server Execute Package Utility Version 10.50.4270.0 for 64-bit Copyright (C) Microsoft Corporation 2010. All rights reserved. Started: 12:26:43 Error: 2015-03-03 12:41:25.79 Code: 0xC001404F
Source: For Loop Container Description: This Variables collection has already been unlocked. The Unlock method is called only once on a dispensed Variables collection. End Error DTExec: The package execution returned DTSER_FAILURE (1). Started: 12:26:43 Finished: 12:41:25 Elapsed: 881.921 seconds. The package execution failed. The step failed.

我是ld感谢任何帮助。

+0

要确定你不会与变量作用域混淆。你可以在一个不同的范围内有两个相同名称的变量,这使得事情变得困惑 – 2015-03-05 11:21:27

+0

@Nick道歉不确定我完全理解。 – Djbril 2015-03-05 11:54:07

+0

在本例中,https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.dts.runtime.variabledispenser.getvariables.aspx这两个变量是首先调用'LockForRead' _before_'GetVariables'。这与你的代码不同。在调用'GetVariables'之前尝试使用'LockForWrite' – 2015-03-05 12:52:56

回答

0

您可能在脚本任务编辑器读/写变量列表(“User :: FileExistsFlg”)中列出了变量,然后在代码中再次锁定它。它的一个或另一个。当我创建了一个两次列出用户变量的示例时,我的SSIS包被挂起了 - 我可能没有等待足够长的时间以致错误出现。 这个工作正常,只要我没有列出变量User :: A1:

Dts.VariableDispenser.LockForWrite(“User :: a1”);

 // Create a variables 'container' to store variables 
     Variables vars = null; 

     // Add variables from the VariableDispenser to the variables 'container' 
     Dts.VariableDispenser.GetVariables(ref vars); 

     //string filepath; 
     // filepath = vars["User::FolderPath"].Value.ToString(); 
     // vars["User::FileExistsFlg"].Value = File.Exists(filepath); 
     vars["User::a1"].Value = 35; 
     MessageBox.Show(vars["User::a1"].Value.ToString()); 
     // Release the locks 
     vars.Unlock(); 

     Dts.TaskResult = (int)ScriptResults.Success; 
+0

我没有在脚本任务编辑器中列出变量。 – Djbril 2015-03-03 15:29:01