我有一个表格,需要对 2 列(工资和奖金)求和并通过电子邮件发送给他们 关闭使用SSIS。我设法编写了一个脚本来发送电子邮件,但无法在 C# 中对 2 列求和。
public void Main()
{
String SendMailFrom = Dts.Variables["EmailFrom"].Value.ToString();
String SendMailTo = Dts.Variables["EmailTo"].Value.ToString();
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("bbb"));
msg.From = new MailAddress("ccc");
msg.Body = "Process is completed successfully.
1) Sum of Salary is 1234
2)Sum of Bonus is 1234
3) Count of distinct Accounts is 123";//This is the requirement
msg.Subject = "XYZ PROCESS";
msg.IsBodyHtml = true;
DataTable dtResults = new DataTable();
OleDbConnection dbConnection = new OleDbConnection("xxx");
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("xxx");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
dbConnection.Open();
if (dbConnection.State == ConnectionState.Open)
{
OleDbCommand dbCommand = dbConnection.CreateCommand();
dbCommand.CommandType = CommandType.Text;
dbCommand.CommandText = "SELECT SUM([Salary]),Sum([Bonus]) FROM table";
OleDbDataReader dbReader = dbCommand.ExecuteReader();
if (dbReader.HasRows)
dtResults.Load(dbReader);
string theSum = dtResults.Rows[0]["TOTAL"].ToString();
dbReader.Close();
dbConnection.Close();
client.Send(msg);
MessageBox.Show("Email was Successfully Sent ");
}
}
//try
//{
// client.Send(msg);
// MessageBox.Show("Email was Successfully Sent ");
//}
catch (Exception ex)
{
throw new Exception("Unable to execute query as requested.", ex);
MessageBox.Show(ex.ToString());
}
//{
// MessageBox.Show(ex.ToString());
//}
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
我不是编写 C# 脚本的专家,希望得到帮助
最佳答案
一如既往,将您的问题分解为更小的单元,直到找到可以解决的问题。
我将创建一个名为 BuildMessageBody 的方法。这将运行您的查询并切出您想要的元素,然后使用 string.Format 方法按序号位置替换您想要的值。
public string BuildMessageBody()
{
// Create a message template that will
string template = @"Process is completed successfully.
1) Sum of Salary is {0}
2) Sum of Bonus is {1}
3) Count of distinct Accounts is {2}";
string query = @"SELECT SUM(T.[Salary]) AS TotalSalary, Sum(T.[Bonus]) AS TotalBonus, COUNT(DISTINCT T.AccountNumber) AS UniqueAccounts FROM table AS T";
string totalSalary = string.Empty;
string totalBonus = string.Empty;
string uniqueAccounts = string.Empty;
string body = string.Empty;
using(OleDbConnection dbConnection = new OleDbConnection("xxx"))
{
dbConnection.Open();
using (OleDbCommand cmd = new OleDbCommand(query, dbConnection))
{
cmd.CommandType = System.Data.CommandType.Text;
using (OleDbDataReader reader = cmd.ExecuteReader())
{
// This should only ever yield one row due to aggregation in source query
// But this implementation will result in the last row (arbitrary source sorting)
// being preserved
while (reader.Read())
{
// Access by ordinal position
totalSalary = reader[0].ToString();
totalBonus = reader[1].ToString();
uniqueAccounts = reader[2].ToString();
}
}
}
// At this point, we should have results
body = string.Format(template, totalSalary, totalBonus, uniqueAccounts);
}
return body;
}
然后,您的原始代码将 msg.Body
的分配替换为
msg.Body = BuildMessageBody();
然后您可以删除 ScriptMain(try block )中的所有数据访问代码,但保留 client.Send(msg);
否则您将永远不会发送电子邮件。
关于c# - 对 SSIS 中的列求和并发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44028328/