3

I have an SSIS package that runs an SQL query and exports it to a csv file via a Data Flow Task. After the csv is created, I have a "Script Task" set to connect to an SMTP server and send the csv file as an attachment.

On my local machine the package runs fine, but when I load it into SQL Server Management Studio on the server it doesn't work as expected. SQL Server MS says that the package executed successfully, and the csv file is generated in the location expected. However, the "Script Task" doesn't appear to be executing at all. I've included some statements in the C# script to write to a file for debugging purposes - one for the try/catch exception block and a couple of others for normal execution.

    public void Main()
    {
        string sSubject = "Weekly PAX Test";
        string sBody = "Test Message";
        int iPriority = 2;

        if (SendMail(sSubject, sBody, iPriority))
        {
            Dts.TaskResult = (int)ScriptResults.Success;
        }
        else
        {
            //Fails the Task
            Dts.TaskResult = (int)ScriptResults.Failure;
        }
    }

    public bool SendMail(string sSubject, string sMessage, int iPriority)
    {
        try
        {
            string sEmailServer = Dts.Variables["User::sEmailServer"].Value.ToString();
            string sEmailPort = Dts.Variables["User::sEmailPort"].Value.ToString();
            string sEmailUser = Dts.Variables["User::sEmailUser"].Value.ToString();
            string sEmailPassword = Dts.Variables["User::sEmailPassword"].Value.ToString();
            string sEmailSendTo = Dts.Variables["User::sEmailSendTo"].Value.ToString();
            string sEmailSendFrom = Dts.Variables["User::sEmailSendFrom"].Value.ToString();
            string sEmailSendFromName = Dts.Variables["User::sEmailSendFromName"].Value.ToString();
            string sAttachmentPath = Dts.Variables["User::sAttachmentPath"].Value.ToString();

            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            MailAddress fromAddress = new MailAddress(sEmailSendFrom, sEmailSendFromName);

            //You can have multiple emails separated by ;
            string[] sEmailTo = Regex.Split(sEmailSendTo, ";");
            int sEmailServerSMTP = int.Parse(sEmailPort);

            smtpClient.Host = sEmailServer;
            smtpClient.Port = sEmailServerSMTP;

            System.Net.NetworkCredential myCredentials =
               new System.Net.NetworkCredential(sEmailUser, sEmailPassword);
            smtpClient.Credentials = myCredentials;

            message.From = fromAddress;

            if (sEmailTo != null)
            {
                for (int i = 0; i < sEmailTo.Length; ++i)
                {
                    if (sEmailTo[i] != null && sEmailTo[i] != "")
                    {
                        message.To.Add(sEmailTo[i]);
                    }
                }
            }

            switch (iPriority)
            {
                case 1:
                    message.Priority = MailPriority.High;
                    break;
                case 3:
                    message.Priority = MailPriority.Low;
                    break;
                default:
                    message.Priority = MailPriority.Normal;
                    break;
            }

            //You can enable this for Attachments.  
            //sAttachmentPath is a string variable for the file path.

            Attachment myAttachment = new Attachment(sAttachmentPath);
            message.Attachments.Add(myAttachment);


            message.Subject = sSubject;
            message.IsBodyHtml = true;
            message.Body = sMessage;

            smtpClient.Send(message);
            System.IO.File.WriteAllText("C:\\Users\\SQLCLservice\\SQLServerAgent\\file.txt", "Test");
            return true;
        }
        catch (Exception ex)
        {
            System.IO.File.WriteAllText("C:\\Users\\SQLCLservice\\SQLServerAgent\\ex.txt", ex.ToString());
            return false;

        }
    }

No email is being sent - no files are being written. It's as if the task is not running at all despite the "Successful Execution".

I did notice that the SQL Server Integration Services 11.0 service is running on my local machine but not on the server. However, if I disable this service on my local machine the task still executes.

Am I missing something else? I'm pretty new to SQL Server and I've been working on this problem for days.

EDIT: I'm running SQL Server 2012

EDIT2: I should also mention that I've tried both saving the package with 64-bit runtime set to false and running it in 32-bit mode through the SQL Server Agent.

4

3 回答 3

9

我之前遇到过类似的情况,在 SSDT(SQL Server Data Tools - 这是 Visual Studio 界面)中一切正常,当我的包部署在 SSMS 中时,只是脚本任务失败了。

使用 SSIS 2012,我加载了一个 Excel 工作表,然后在脚本任务中,我调用了一个 Excel 宏来对工作表中的差异进行排序和突出显示。在 SSDT 环境中一切正常,但是当我在 SSMS 中运行(作为计划作业 - 32 位模式)时,脚本任务没有执行。但是,我可以看到加载了原始数据的 Excel 工作表 - 没有排序和突出显示差异。

SSMS 包执行日志中未捕获任何错误。正如@Tab Alleman 在评论部分强调的那样,一些错误记录在事件查看器中。在我的情况下,错误记录在WindowsLogs > System

在此处输入图像描述

在访问 Microsoft SQL Server Integration Services 11.0 时显示 SQLSERVERAGENT 的权限错误。

特定于应用程序的权限设置不会将具有 CLSID {FDC3723D-1588-4BA3-92D4-42C430735D7D} 和 APPID {83B33982-693D-4824-B42E-7196AE61BB05} 的 COM 服务器应用程序的本地激活权限授予用户 NT SERVICE\SQLSERVERAGENT

然后在授予适当的访问权限后(此处描述了授予访问权限的步骤),当帐户尝试访问 Excel 时,我又收到了一个权限错误

机器默认权限设置不向用户 NT SERVICE\SQLSERVERAGENT 授予 CLSID {00024500-0000-0000-C000-000000000046} 和 APPID {00020812-0000-0000-C000-000000000046} 的 COM 服务器应用程序的本地激活权限

即使解决了所有权限错误,我仍然看不到预期的输出。所以我增加了脚本任务的自定义日志记录(参考这里的步骤)。然后我能够看到脚本任务中引发的异常。我的 catch 块看起来像下面的那个:

  catch (Exception ex)
            {
               Dts.Log(ex.ToString(), 0, emptyBytes);
            }

它将在 sysssislog 表中记录错误(您必须在配置自定义日志记录时进行配置)

select * from [*YourDatabaseName*].[dbo].[sysssislog]

应该列出您的自定义日志。

于 2015-08-27T03:00:04.423 回答
3

我也遇到过类似的情况,但解决方法不同。我的 SSIS 包中有两个关键任务。一个运行 C# 的脚本任务和一个读取 Excel 文件的数据流任务。我第一次收到这个错误:

OLE DB provider Microsoft.Jet.OLEDB.4.0 is not registered.  If the 64-bit drive is not installed, run the package in 32-bit mode.

在 32 位模式下运行后,我注意到我的脚本任务没有运行。SSIS 只是忽略它,就好像它被禁用一样。我确实在我的事件查看器中发现了一些审核失败。

我的脚本任务未运行的根本原因是因为它是为 x64 编译的(默认设置)。这是我用来解决此问题的步骤:

  1. 编辑脚本
  2. 右键单击看起来像 GUID 的 c# 项目名称并选择属性
  3. 选择属性页左侧的 Build 菜单
  4. 设置平台目标 = x86

现在,您的脚本任务现在将与您的 32 位 OLEDB Jet 驱动程序一起以 32 位模式运行。

于 2016-09-16T22:55:50.123 回答
0

检查项目的构建版本。当您的构建版本大于目录使用的目标服务器版本时,就会发生这种情况。IE。在 Visual Studio 中将 2017 版构建到 2016 sql 服务器。更改项目的构建版本,然后重新编译所有脚本任务。使用 ispac 文件重新部署您的项目。脚本任务将按预期执行。

于 2021-09-03T15:14:04.170 回答