Как использовать переменную SSIS вне метода Main () в Задаче сценария SSIS - PullRequest
2 голосов
/ 28 июня 2019

Я использую задачу сценария 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 голосов
/ 28 июня 2019

Самый простой способ - объявить 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
     }

    }

    }
    }

...