【问题标题】:SSIS is executing successfully, but it doesn't seem to be running the script task portionSSIS 执行成功,但似乎没有运行脚本任务部分
【发布时间】:2015-09-26 14:04:32
【问题描述】:

我有一个 SSIS 包,它运行 SQL 查询并通过数据流任务将其导出到 csv 文件。创建 csv 后,我设置了“脚本任务”以连接到 SMTP 服务器并将 csv 文件作为附件发送。

在我的本地计算机上,包运行良好,但是当我将它加载到服务器上的 SQL Server Management Studio 时,它无法按预期工作。 SQL Server MS 表示包执行成功,并且在预期的位置生成了 csv 文件。但是,“脚本任务”似乎根本没有执行。我在 C# 脚本中包含了一些语句来写入文件以进行调试 - 一个用于 try/catch 异常块,另外几个用于正常执行。

    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;

        }
    }

没有发送电子邮件 - 没有写入文件。尽管“成功执行”,但好像任务根本没有运行。

我确实注意到 SQL Server Integration Services 11.0 服务正在我的本地计算机上运行,​​但不在服务器上。但是,如果我在本地计算机上禁用此服务,任务仍会执行。

我还缺少其他东西吗?我对 SQL Server 还很陌生,而且我已经解决这个问题好几天了。

编辑:我正在运行 SQL Server 2012

EDIT2:我还应该提到,我已经尝试将 64 位运行时设置为 false 的包保存并通过 SQL Server 代理在 32 位模式下运行它。

【问题讨论】:

  • 你检查过服务器错误日志吗?
  • 包的日志显示成功执行。我不确定我应该检查哪些其他日志。
  • 您可以查看服务器上的 windows 事件日志。另见此处:msdn.microsoft.com/en-us/library/ms140246(v=sql.110).aspx
  • 'Package "WeeklyReportTest" 成功完成'是我在 Windows 事件日志中看到的唯一日志。
  • 好吧,您可以尝试在事件处理程序中放置一些通知,用于脚本任务的执行前和执行后,以验证它在服务器上执行时是否到达那里。

标签: c# sql-server csv ssis script-task


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我也遇到过类似的情况,但解决方法不同。我的 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. 选择属性页面左侧的构建菜单
    4. 设置平台目标 = x86

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

    【讨论】:

      【解决方案3】:

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

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

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

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

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

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

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

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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        • 2015-10-28
        • 1970-01-01
        • 1970-01-01
        • 2019-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多