在指令碼工作中引發事件
事件會提供向包含封裝報告錯誤、警告和其他資訊 (例如工作進度或狀態) 的方法。封裝會提供管理事件通知的事件處理常式。指令碼工作可以呼叫 Dts 物件之 Events 屬性上的方法來引發事件。如需有關 Integration Services 封裝如何處理事件的詳細資訊,請參閱<Integration Services 事件處理常式>。
事件可以記錄到封裝中啟用的任何記錄提供者。記錄提供者會在資料存放區中儲存事件的相關資訊。指令碼工作也可以使用 Log 方法將資訊記錄到記錄提供者,而不會引發事件。如需有關如何使用 Log 方法的詳細資訊,請參閱<在指令碼工作中記錄>。
為了引發事件,指令碼工作會呼叫 Events 屬性所公開的其中一個方法。下表列出 Events 屬性所公開的方法。
事件 |
描述 |
---|---|
引發封裝中使用者定義的自訂事件。 |
|
通知封裝有關錯誤狀況。 |
|
提供資訊給使用者。 |
|
通知封裝有關工作的進度。 |
|
傳回一個值,指出此封裝是否需要提前關閉工作。 |
|
通知封裝此工作是在需要使用者通知的狀態,但不是錯誤狀況。 |
事件範例
下列範例會示範如何在指令碼工作內引發事件。此範例會使用原生 Windows API 函數來判斷是否可使用網際網路連接。如果無任何可用的連接,則會引發錯誤。如果正在使用可能不穩定的數據機連接,此範例會引發警告。否則,它會傳回參考用訊息,表示已偵測到網際網路連接。
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;
}
|