[英]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.