如何在SSIS脚本任务的Main()方法之外使用SSIS变量

时间:2019-06-27 21:47:12

标签: c# variables ssis etl script-task

我正在使用SSIS脚本任务来基于一些前提条件发送自动电子邮件。作为其一部分,我有一个SendAutomatedEmail()方法,在此方法中,我传递了两个变量mailServer和收件人。这样做会遇到错误“对象引用未设置为对象实例”。

试图使用构造函数,但不能解决问题。

class Program
{
    public void Main()
    {
        string mailServer = Dts.Variables["User::varServer"].Value.ToString();  
        string recipient = Dts.Variables["User::varRecipient"].Value.ToString(); 

        server msr = new server(mserv, rec);
    }

    public class server
    {
        string ms;
    string r;

        public result(string mserv, string rec)
        {
           ms = mserv;
           r = rec;
        }
    }
}

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    public void Main()
    {
    try
    {
        //do something
    }
    catch
    {
        //catch exception
    }
    }

public static void SendAutomatedEmail(string htmlString, string recipient = "user@domain.com")
{

 try
 {
     string mailServer = Dts.Variables["User::varServer"].Value.ToString();  //error "object reference not set to an instance of an object."
     string recipient = Dts.Variables["User::varRecipient"].Value.ToString();   //error "object reference not set to an instance of an object."

     MailMessage message = new MailMessage("it@domain.com", recipient);
     message .IsBodyHtml = true;
     message .Body = htmlString;
     message .Subject = "Test Email";

     SmtpClient client = new SmtpClient(mailServer);
     var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
     client.Credentials = AuthenticationDetails;
     client.Send(message);
 }
 catch (Exception e)
 {
     //catch exception
 }

}

}

我应该能够在SendAutomatedEmail()方法中将值无缝地传递给变量。

1 个答案:

答案 0 :(得分:0)

最简单的方法是在程序类中声明2个变量,读取Main()函数中的值,并将这些值分配给局部变量。然后,您可以在Main()函数之外使用局部变量。

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{

    string mailServer;
     string recipient;

    public void Main()
    {
    try
    {
        mailServer = Dts.Variables["User::varServer"].Value.ToString();
        recipient = Dts.Variables["User::varRecipient"].Value.ToString();

    }
    catch
    {
        //catch exception
    }
    }

 private class sendEMail
 {
    public static void SendAutomatedEmail(string htmlString, string recipient = "user@domain.com")
    {

     try
     {

         MailMessage message = new MailMessage("it@domain.com", recipient);
         message .IsBodyHtml = true;
         message .Body = htmlString;
         message .Subject = "Test Email";

         SmtpClient client = new SmtpClient(mailServer);
         var AuthenticationDetails = new NetworkCredential("user@domain.com", "password");
         client.Credentials = AuthenticationDetails;
         client.Send(message);
     }
     catch (Exception e)
     {
         //catch exception
     }

    }

    }
    }