尝试锁定变量时检测到死锁。 16次尝试后无法获取锁定。锁定超时

时间:2017-10-13 20:35:19

标签: ssis ssis-2012

我尝试使用SSIS在电子邮件中发送一组查询结果。以下是屏幕截图和流程步骤。

截图:

enter image description here

步骤: 1.使用SQL任务在" queryresult"中获取查询结果。对象变量 2.使用" queryresult" Foreach循环中的对象变量并获取字符串变量中的列值" ProjectRefID"和"帐号"

enter image description here

3.在foreachloop容器中使用脚本任务来捕获对象变量中的数据"查询结果"

enter image description here

  1. 以下是我从互联网上复制的脚本任务中的代码。

    使用System; 使用System.Data; 使用Microsoft.SqlServer.Dts.Runtime; 使用System.Windows.Forms;

    endregion

    namespace ST_77e6b4ea18824b909adc5568475fcf5c
     {
    
       [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    
    
    
    {
    
        public void Main()
        {
            Variables varCollection = null;
            string header = string.Empty;
            string message = string.Empty;
    
            Dts.VariableDispenser.LockForWrite("User::ValidationEmailMessage");
            Dts.VariableDispenser.LockForRead("User::Accountnumber");
            Dts.VariableDispenser.LockForRead("User::ProjectRefID");
            Dts.VariableDispenser.GetVariables(ref varCollection);
    
            if (varCollection["User::ValidationEmailMessage"].Value == string.Empty)
            {
                header = "Below are the list of Invalid ProjecRefID and Accountnumbers that are not matching with our existing data:\n\n";
                header += string.Format("{0}\t{1}\t\t\t{2}\n", "ProjectRefID", "Accountnumber");
                varCollection["User::ValidationEmailMessage"].Value = header;
                varCollection.Unlock();
            }
    
    
            //Format the query result with tab delimiters
            message = string.Format("{0}\t{1}\t{2}",
                                        varCollection["User::ProjectRefID"].Value,
                                        varCollection["User::Accountnumber"].Value);
    
            varCollection["User::ValidationEmailMessage"].Value = varCollection["User::ValidationEmailMessage"].Value + message;
            varCollection.Unlock();
            Dts.TaskResult = (int)ScriptResults.Success;
    
        }
    
    
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
    
    
    }
    

    }

  2. 我试图解决这个错误,但不知怎的,我无法解决这个问题。如果有人知道如何解决它,请告诉我。

1 个答案:

答案 0 :(得分:2)

除非您出于某种原因需要锁定功能,否则您应该能够像这样编写Main方法:

public void Main()
{
    string header = string.Empty;
    string message = string.Empty;

    if (Dts.Variables["User::ValidationEmailMessage"].Value == string.Empty)
    {
        header = "Below are the list of Invalid ProjecRefID and Accountnumbers that are not matching with our existing data:\n\n";
        header += string.Format("{0}\t{1}\t\t\t{2}\n", "ProjectRefID", "Accountnumber");
        Dts.Variables["User::ValidationEmailMessage"].Value = header;
    }


    //Format the query result with tab delimiters
    message =
        string.Format("{0}\t{1}\t{2}",
            Dts.Variables["User::ProjectRefID"].Value,
            Dts.Variables["User::Accountnumber"].Value);

    Dts.Variables["User::ValidationEmailMessage"].Value = Dts.Variables["User::ValidationEmailMessage"].Value + message;

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

此外,使用string.Format代码,您指定了三个索引{0},{1}和{2},但您只提供了2个参数,即, "ProjectRefID", "Accountnumber");