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

A deadlock was detected while trying to lock variables. A lock cannot be acquired after 16 attempts. The locks timed out

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

截图:

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

中的列Values

3.Using foreachloop容器内的脚本任务,抓取对象变量中的数据"query result"

  1. 下面是我从网上复制的脚本任务里面的代码

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

    结束区域

    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
        };
    
    
    }
    

    }

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

除非出于某种原因需要锁定功能,否则您应该能够像这样简单地编写 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");