简体   繁体   中英

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM