[英]Reports job scheulded on server works different from my local machine
here is the brief description for the issue: 这是该问题的简要说明:
I have a script that written in C#
with HTML/CSS
and TSQL
language, the script is supposed to distribute the generated report to each of our team member, when I test on my local machine, everyone could get the email, but if I scheduled it as a job on our server, only few of us could get (we have a table for logging the sent email, and the logging step is the TSQL Stored Procedure in the script,too), I've checked the table, and there is no such data records for missing recipient, only for those who got the email, which means the Stored Procedure did not work for those members who did not get the email, and below is the code for logging to the table and send out the email 我有一个用C#
用HTML/CSS
和TSQL
语言编写的脚本,该脚本应该将生成的报告分发给我们的每个团队成员,当我在本地计算机上进行测试时,每个人都可以收到电子邮件,但是如果我计划了作为服务器上的一项工作,我们中只有少数人可以得到(我们有一个用于记录已发送电子邮件的表,并且记录步骤也是脚本中的TSQL存储过程),我已经检查了该表,没有丢失收件人的此类数据记录,仅适用于收到电子邮件的人员,这意味着存储过程不适用于未收到电子邮件的成员,以下是用于登录到表并发送电子邮件的代码
public void Main()
{
...code above
try
{
...code above
if (SendMail(sSubject, sBody))
{
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
Dts.TaskResult = (int)ScriptResults.Failure;
}
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
//error handling
}
}
public bool SendMail(string sSubject, string sMessage)
{
try
{
string sEmailSendTo = Dts.Variable["ProjRespEmail"].Value.ToString();
string sEmailSendFrom = Dts.Variables["SqlEmail"].Value.ToString();
if (Dts.Variables["StrClientName"].Value.ToString() != "")
{
using (SqlConnection sqlCon = new SqlConnection("connection name"))
{
sqlCon.Open();
using (SqlCommand sqlCmd = sqlCon.CreateCommand())
{
sqlCmd.CommandText = "name";
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("SendingMachine", Environment.MachineName));
sqlCmd.Parameters.Add(new SqlParameter("Recipients", sEmailSendTo));
sqlCmd.Parameters.Add(new SqlParameter("Subject", sSubject));
sqlCmd.Parameters.Add(new SqlParameter("Body", sMessage));
sqlCmd.ExecuteNonQuery();
}
}
}
Dts.Variables["ProjRespEmail"].Value = "";
Dts.Variables["StrProjResponsible"].Value = "";//SqlEmail
Dts.Variables["SqlEmail"].Value = "";
Dts.Variables["StrClientName"].Value = "";
return true;
}
catch (Exception ex)
{
//error handling
return false;
}
}
Any suggestion would be much appreciated. 任何建议将不胜感激。
Part of the issue in the your code is that the TaskResult is always set to Success in the end. 您的代码中的部分问题是,最后总是将TaskResult设置为Success。 Remove the line noted below: 删除下面指出的行:
try
{
...code above
if (SendMail(sSubject, sBody))
{
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
Dts.TaskResult = (int)ScriptResults.Failure;
}
//Remove this, because it overwrites the actions from above
//Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception e)
{
//error handling
}
This will at least allow the task to fail so you can capture any errors. 这至少将使任务失败,因此您可以捕获任何错误。 By your description, it sounds like the proc might differ from your local machine to the server's version in that the Recipients variable might differ in size and be truncating the list. 根据您的描述,听起来proc可能与本地计算机和服务器版本不同,因为Recipients变量的大小可能不同,并且会截断列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.