【问题标题】:How to use SSIS variable outside Main() method in SSIS Script Task如何在 SSIS 脚本任务中的 Main() 方法之外使用 SSIS 变量
【发布时间】:2019-11-09 22:26:30
【问题描述】:

我正在使用 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() 方法中无缝地将值传递给变量。

【问题讨论】:

  • 只需从函数中删除static 选项

标签: c# variables ssis etl script-task


【解决方案1】:

最简单的方法是在 Program Class 中声明 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
     }

    }

    }
    }

【讨论】:

  • 嗨@Hadi,我之前确实尝试过这个并且得到了这个错误CS0120:非静态字段、方法或属性需要对象引用。
  • 从函数中移除static选项
  • @user25407 使用public void SendAutomatedEmail()
  • 嗨@Hadi,函数 SendAutomatedEmail() 在不同的私有类中。
  • @user25407 然后保持代码不变并尝试删除static 选项
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2023-01-09
  • 2013-09-13
  • 1970-01-01
相关资源
最近更新 更多