使用事件提交預存程序
Notification Services 提供了事件收集預存程序,供您提交單一事件或事件批次。請利用這些預存程序和 Transact-SQL 程式碼來提交事件。您可以手動執行這些預存程序、在 Microsoft SQL Server Agent 作業中執行這些預存程序,或在觸發程序中使用這些預存程序,以便執行查詢來回應另一份資料表的插入或更新作業。
提交事件資料
您可以利用一組預存程序來開啟事件批次,個別提交一或多個事件,再關閉事件批次。請利用下列預存程序,將個別事件提交給 Notification Services 應用程式:
- NSEventBeginBatchEventClassName 以提交事件資料的事件提供者名稱為輸入引數,再傳回新事件批次的批次識別碼。
- NSEventWriteEventClassName 會將單一事件加入事件批次中。這些預存程序引數是事件批次識別碼及事件欄位的值清單。
- NSEventFlushBatchEventClassName 會關閉事件批次,以及將事件集提交給應用程式。
請務必在單一交易內執行所有這些預存程序,使 Notification Services 能夠在相同交易內開啟批次、提交事件,再關閉批次。
如需詳細資訊和範例,請參閱下列預存程序主題:
- NSEventBeginBatch<EventClassName> (Transact-SQL)
- NSEventWrite<EventClassName> (Transact-SQL)
- NSEventFlushBatch<EventClassName> (Transact-SQL)
利用查詢來收集和提交事件
如果您要利用 SELECT 陳述式來收集事件,您可以使用 NSEventSubmitBatchEventClassName 預存程序。這個預存程序使用兩項查詢:一項是利用您定義的查詢來收集事件,另一項是在收集事件之後執行的後收集查詢 (稱為*「後查詢」*)。您可以利用這個後查詢來執行任何必要的清除工作,例如,將追蹤欄位從 "new" 改成 "collected",以避免重複收集相同的資料。
如需詳細資訊和範例,請參閱<NSEventSubmitBatch<EventClassName> (Transact-SQL)>。
在應用程式中使用事件提交預存程序
您可以從 Managed 和 Unmanaged 程式碼內執行事件收集預存程序。從 Managed 程式碼內執行預存程序的常見方式之一,是使用 SqlCommand 物件。您可以在 SqlCommand 物件中指定預存程序及其引數,再利用它的 ExecuteNonQuery 方法來執行程序。
範例:加入單一事件
這個範例使用下列命名空間:
public bool EventSPs()
{
// These variables would normally be defined for
// the class. They would be set based on the values
// provided by the args argument of the Initialize method.
string instanceName = "Tutorial";
string applicationName = "Weather";
string eventClassName = "WeatherEvents";
string eventProviderName = "WeatherSPs";
bool returnValue = true;
SqlConnection databaseConnection = null;
try
{
// Set the connection to SQL Server.
databaseConnection = new SqlConnection();
// Build the connection string.
StringBuilder connectBuilder = new StringBuilder();
connectBuilder.Append("Integrated Security=SSPI;");
connectBuilder.Append("Data Source=MyServer;");
connectBuilder.Append("Initial Catalog=TutorialWeather");
databaseConnection.ConnectionString =
connectBuilder.ToString();
// Open the connection.
databaseConnection.Open();
// Use NSEventBeginBatchEventClassName
// to create a new event batch and return the ID.
SqlCommand databaseCommand = new SqlCommand(string.Format
("\"NSEventBeginBatch{0}\"", eventClassName));
databaseCommand.Connection = databaseConnection;
databaseCommand.CommandType = CommandType.StoredProcedure;
databaseCommand.Parameters.AddWithValue
("@ProviderName", eventProviderName);
SqlParameter storedProcParameter =
databaseCommand.Parameters.Add
("@EventBatchId", SqlDbType.BigInt);
storedProcParameter.Direction = ParameterDirection.Output;
databaseCommand.ExecuteNonQuery();
long eventBatchId =
(long)databaseCommand.Parameters["@EventBatchId"].Value;
// Use NSEventWriteEventClassName
// to write the event to the database.
databaseCommand.Parameters.Clear();
databaseCommand.CommandText =
string.Format("\"NSEventWrite{0}\"", eventClassName);
databaseCommand.Parameters.AddWithValue("@EventBatchId",
eventBatchId);
databaseCommand.Parameters.AddWithValue("@City", "Redmond");
databaseCommand.Parameters.AddWithValue("@Date", "4/5/05");
databaseCommand.Parameters.AddWithValue("@Low", 50.0);
databaseCommand.Parameters.AddWithValue("@High", 55.5);
databaseCommand.Parameters.AddWithValue("@Forecast",
"Partly cloudy");
Console.WriteLine(databaseCommand.CommandText);
databaseCommand.ExecuteNonQuery();
// Use NSEventFlushBatchEventClassName
// to commit the event batch.
databaseCommand.Parameters.Clear();
databaseCommand.CommandText =
string.Format("\"NSEventFlushBatch{0}\"", eventClassName);
databaseCommand.Parameters.AddWithValue("@EventBatchId",
eventBatchId);
long eventsSubmitted = (long)databaseCommand.ExecuteScalar();
}
catch(SqlException ex)
{
Console.WriteLine(ex);
}
finally
{
if (null != databaseConnection)
{
databaseConnection.Close();
databaseConnection = null;
}
}
return true;
}
範例:加入多個事件
這個範例使用下列命名空間:
- System
- System.Text
- System.Data
- System.Data.SqlClient
- Microsoft.SqlServer.NotificationServices
public bool EventQuery()
{
// These variables would normally be defined for
// the class. They would be set based on the values
// provided by the args argument of the Initialize method.
string instanceName = "Tutorial";
string applicationName = "Weather";
string eventClassName = "WeatherEvents";
string eventProviderName = "WeatherSPs";
StringBuilder builder =
new StringBuilder("SELECT City, GetDate() AS Date, ");
builder.Append("Low, High, Forecast ");
builder.Append("FROM dbo.WeatherData;");
string eventsQuery = builder.ToString();
bool returnValue = true;
SqlConnection databaseConnection = null;
try
{
// Set the connection to SQL Server.
databaseConnection = new SqlConnection();
// Build the connection string.
StringBuilder connectBuilder = new StringBuilder();
connectBuilder.Append("Integrated Security=SSPI;");
connectBuilder.Append("Data Source=MyServer;");
connectBuilder.Append("Initial Catalog=TutorialWeather");
databaseConnection.ConnectionString =
connectBuilder.ToString();
// Open the connection.
databaseConnection.Open();
// Use NSEventSubmitBatchEventClassName
// to create and submit a batch of events.
SqlCommand databaseCommand = new SqlCommand(string.Format
("\"NSEventSubmitBatch{0}\"", eventClassName));
databaseCommand.Connection = databaseConnection;
databaseCommand.CommandType = CommandType.StoredProcedure;
databaseCommand.Parameters.AddWithValue
("@ProviderName", eventProviderName);
databaseCommand.Parameters.AddWithValue("@EventsQuery",
eventsQuery);
databaseCommand.Parameters.AddWithValue("@PostQuery", " ");
long eventsInBatch = (long)databaseCommand.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex);
}
finally
{
if (null != databaseConnection)
{
databaseConnection.Close();
databaseConnection = null;
}
}
return true;
}
請參閱
其他資源
開發自訂事件提供者
Notification Services 預存程序 (Transact-SQL)