使用事件提交存储过程

Notification Services 提供事件收集存储过程,这些存储过程使得您能够提交单个事件或事件批次。使用这些存储过程可通过 Transact-SQL 代码提交事件。您可以手动运行这些存储过程,在 Microsoft SQL Server 代理作业中运行这些存储过程,还可以在触发器中使用这些存储过程以便运行查询来响应对另一个表执行的插入或更新操作。

提交事件数据

使用一组存储过程可以打开事件批次,单独提交一个或多个事件,然后关闭事件批次。使用下列存储过程,可将单独的事件提交到 Notification Services 应用程序:

  • NSEventBeginBatchEventClassName 将提交事件数据的事件提供程序的名称作为输入参数,然后返回新事件批次的批 ID。
  • NSEventWriteEventClassName 将单个事件添加到事件批次。这些存储过程参数是事件批次 ID 和事件字段的值列表。
  • NSEventFlushBatchEventClassName 关闭事件批次并将事件集提交到应用程序

确保在同一事务中执行所有这些存储过程,从而使 Notification Services 在一个事务中打开事件批次,提交事件,然后关闭事件批次。

有关详细信息和示例,请参阅以下存储过程主题:

使用查询收集和提交事件

如果希望使用 SELECT 语句收集事件,可使用 NSEventSubmitBatchEventClassName 存储过程。该存储过程使用两个查询:一个查询使用您定义的查询收集事件,一个收集之后查询(称为“后查询**”)在收集事件后运行。您可使用该后查询执行任何必要的清除,例如将跟踪字段从“new”更改为“collected”,从而不必再次收集相同数据。

有关详细信息和示例,请参阅 NSEventSubmitBatch<EventClassName> (Transact-SQL)

在应用程序中使用事件提交存储过程

可以在托管代码和非托管代码中运行事件收集存储过程。在托管代码中执行存储过程的一个常用方法是使用 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)

帮助和信息

获取 SQL Server 2005 帮助