在脚本任务中引发事件

事件提供向包含包报告错误、警告和其他信息(如任务进度或状态)的方式。包为管理事件通知提供事件处理程序。脚本任务可以通过对 Dts 对象的 Events 属性调用方法来引发事件。有关 Integration Services 包如何处理事件的详细信息,请参阅 Integration Services 事件处理程序

事件可以记录到包中已启用的任何日志提供程序中。日志提供程序在数据存储区中存储有关事件的信息。脚本任务还可以使用 Log 方法将信息记录到日志提供程序中而不引发事件。有关如何使用 Log 方法的详细信息,请参阅脚本任务中的日志记录

为了引发事件,脚本任务将调用由 Events 属性公开的方法之一。下表列出了由 Events 属性公开的方法。

事件

说明

FireCustomEvent

引发包中用户定义的自定义事件。

FireError

将错误情况通知包。

FireInformation

为用户提供信息。

FireProgress

将任务的进度通知包。

FireQueryCancel

返回一个指示包是否需要提前关闭任务的值。

FireWarning

向包发出通知:任务处于有必要发出用户通知,但不是错误条件的状态。

事件示例

下面的示例演示如何从脚本任务内部引发事件。该示例使用本机 Windows API 函数确定 Internet 连接是否可用。如果没有可用的连接,则引发错误。如果使用的调制解调器连接可能不稳定,则该示例将引发警告。否则,返回已检测到 Internet 连接的信息性消息。

Private Declare Function InternetGetConnectedState Lib "wininet" _
    (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long

Private Enum ConnectedStates
    LAN = &H2
    Modem = &H1
    Proxy = &H4
    Offline = &H20
    Configured = &H40
    RasInstalled = &H10
End Enum

Public Sub Main()

    Dim dwFlags As Long
    Dim connectedState As Long
    Dim fireAgain as Boolean

    connectedState = InternetGetConnectedState(dwFlags, 0)

    If connectedState <> 0 Then
        If (dwFlags And ConnectedStates.Modem) = ConnectedStates.Modem Then
            Dts.Events.FireWarning(0, "Script Task Example", _
                "Volatile Internet connection detected.", String.Empty, 0)
        Else
            Dts.Events.FireInformation(0, "Script Task Example", _
                "Internet connection detected.", String.Empty, 0, fireAgain)
        End If
    Else
        ' If not connected to the Internet, raise an error.
        Dts.Events.FireError(0, "Script Task Example", _
            "Internet connection not available.", String.Empty, 0)
    End If

    Dts.TaskResult = ScriptResults.Success

End Sub
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class ScriptMain
{


[DllImport("wininet")]
        private extern static long InternetGetConnectedState(ref long dwFlags, long dwReserved);

        private enum ConnectedStates
        {
            LAN = 0x2,
            Modem = 0x1,
            Proxy = 0x4,
            Offline = 0x20,
            Configured = 0x40,
            RasInstalled = 0x10
        };

        public void Main()
        {
            //
            long dwFlags = 0;
            long connectedState;
            bool fireAgain = true;
            int state;

            connectedState = InternetGetConnectedState(ref dwFlags, 0);
            state = (int)ConnectedStates.Modem;
            if (connectedState != 0)
            {
                if ((dwFlags & state) == state)
                {
                    Dts.Events.FireWarning(0, "Script Task Example", "Volatile Internet connection detected.", String.Empty, 0);
                }
                else
                {
                    Dts.Events.FireInformation(0, "Script Task Example", "Internet connection detected.", String.Empty, 0, ref fireAgain);
                }
            }
            else
            {
                // If not connected to the Internet, raise an error.
                Dts.Events.FireError(0, "Script Task Example", "Internet connection not available.", String.Empty, 0);
            }

            Dts.TaskResult = (int)ScriptResults.Success;

        }
Integration Services 图标(小) 使 Integration Services 保持最新

若要从 Microsoft 获得最新的下载内容、文章、示例和视频,以及从社区获得所选解决方案,请访问 MSDN 或 TechNet 上的 Integration Services 页:

若要获得有关这些更新的自动通知,请订阅该页上提供的 RSS 源。