SSIS 脚本任务抛出未设置为对象实例的对象引用

Posted

技术标签:

【中文标题】SSIS 脚本任务抛出未设置为对象实例的对象引用【英文标题】:SSIS Script Task throws Object reference not set to an instance of an object 【发布时间】:2022-01-08 09:14:45 【问题描述】:

我正在尝试查询数据库并发送电子邮件正文中的内容。当我尝试运行包时,它会引发死锁错误。谁能建议我错过了什么

脚本如下

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Data.OleDb;
using System.Net.Mail;
using System.Net;
#endregion

namespace ST_c074d0acfee7488b96d42a0f858efee7

    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    

        enum ScriptResults
        
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        

        public void Main()
        
            Variables varCollection = null;

            string User_Recepient_Email_ID = Dts.Variables["User::UserEml"].Value.ToString();

            //Dts.VariableDispenser.LockForWrite("User::EmailData");
            //Dts.VariableDispenser.GetVariables(ref varCollection);
            var data = varCollection["User::EmailData"].Value;

            OleDbDataAdapter da = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            da.Fill(dt, varCollection["User::EmailData"].Value);

            SendMailMessage("loadJob@xyz.com", User_Recepient_Email_ID, "ETL Load Status Report", ConvertDataTableTohtml(dt), true, "smtp.xxxxxxxxxxxxx.org");

            Dts.TaskResult = (int)ScriptResults.Success;
        


        public static string ConvertDataTableToHTML(DataTable dt)
        
            string html = "<table border ='1'>";
            //add header row
            html += "<tr>";
            for (int i = 0; i < dt.Columns.Count; i++)
                html += "<th>" + dt.Columns[i].ColumnName + "</th>";
            html += "</tr>";
            //add rows
            for (int i = 0; i < dt.Rows.Count; i++)
            
                html += "<tr style='color:blue;'>";
                for (int j = 0; j < dt.Columns.Count; j++)
                    html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
                html += "</tr>";
            
            html += "</table>";
            return html;
        
        private void SendMailMessage(string From, string SendTo, string Subject, string Body, bool IsBodyHtml, string Server)
        
            MailMessage htmlMessage;
            SmtpClient mySmtpClient;

            htmlMessage = new MailMessage(From, SendTo, Subject, Body);
            htmlMessage.IsBodyHtml = IsBodyHtml;

            mySmtpClient = new SmtpClient(Server);
            mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            mySmtpClient.Send(htmlMessage);
        
    

下面是

包装如下所示。对于每个用户,我需要获取所有订单和订单详细信息,并将其作为电子邮件发送给他们。所以我在循环中有脚本

早些时候我遇到了死锁错误,我根据不同的文章更改了一些东西,现在我遇到了错误

Error: 0x1 at Script Task: Object reference not set to an instance of an object.
Task failed: Script Task
Warning: 0x80019002 at Foreach Loop each User: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Warning: 0x80019002 at SurplusMouse_EmailOrderDetail: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.  The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package 

【问题讨论】:

在代码中放一个断点,你会得到一个更好的错误。这个错误真的可以是任何东西。 【参考方案1】:

我的猜测是这是您的错误(varCollection 为 null,而您引用的内容为 null):

var data = varCollection["User::EmailData"].Value;

就像你之前做的那样分配它...

var data = Dts.Variables["User::EmailData"].Value;

但是,我认为您正在尝试这个。电子邮件数据是存储在对象中的记录集。这就是你想要的逻辑。

using System.Data.OleDb;

DataTable dt= new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.Fill(dt, Dts.Variables["User::emailData"].Value);

foreach (DataRow row in dt.Rows)

   //insert what you want to do here

【讨论】:

以上是关于SSIS 脚本任务抛出未设置为对象实例的对象引用的主要内容,如果未能解决你的问题,请参考以下文章

Ldap连接抛出对象引用未设置为对象错误的实例

Roles.GetRolesForUser 抛出异常对象引用未设置为对象的实例

对象引用未设置为在单独项目中从 DAL 抛出的对象的实例

未将对象引用设置到对象的实例

未将对象引用设置到对象的实例

未将对象引用设置到对象的实例