簡體   English   中英

如何在SSIS腳本任務的Main()方法之外使用SSIS變量

[英]How to use SSIS variable outside Main() method in SSIS 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()方法中將值無縫地傳遞給變量。

最簡單的方法是在程序類中聲明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
     }

    }

    }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM