TechTalk Genome v4.2

Context.ConnectionStateChange Event

Occurs before and after the state has been changed for a database connection or transaction that is managed by Genome in this Context.

public event EventHandler<ConnectionEventArgs> ConnectionStateChange;

Implements

IContext.ConnectionStateChange

Remarks

The event can be also handled for a specific DataDomain see the DataDomain.ConnectionStateChange for details.

The sender parameter of the event handler receives the Context being used. The args parameter contains the details of the connection and/or transaction that is affected. The ConnectionEventArgs.Connection property contains an opened ADO.NET IDbConnection instance. The ConnectionEventArgs.Transaction property of the args parameter is a null reference if the connection serves read-only purposes or the event fired before the transaction opening and otherwise contains the related IDbTransaction instance. The ConnectionEventArgs.ConnectionEvent property specifies the connection/transaction event that has been performed. The ConnectionEventArgs.DataDomain property specifies the DataDomain this database connection belongs to. The ConnectionEventArgs.Context property specifies the Context this database connection belongs to. The ConnectionEventArgs.CustomData property points to a custom data storage that is attached to the connection.

Example

          
using(Context.Push(ShortRunningTransactionContext.Create()))
{
    Context.Current.ConnectionStateChange += new EventHandler<ConnectionEventArgs>(Context_ConnectionStateChange);
    //...
}

private void Context_ConnectionStateChange(object sender, ConnectionEventArgs args) 
{
    if (args.ConnectionEvent == DatabaseConnectionEvent.ConnectionOpened)
    {
        SqlCommand command = new SqlCommand(
            "SET LOCK_TIMEOUT 10", 
            (SqlConnection)args.Connection,
            (SqlTransaction)args.Transaction);
        command.ExecuteNonQuery();
    }
}
                
        
The following example shows how to use the sp_setapprole function of Microsoft SQL Server 2005
          
private void InitializeDataDomain()
{
    DataDomain dataDomain = new DataDomain(...);
    //...
    dataDomain.ConnectionStateChange += new EventHandler<ConnectionEventArgs>(DataDomain_ConnectionStateChange);
    //...
}

private void DataDomain_ConnectionStateChange(object sender, ConnectionEventArgs args) 
{
    if (e.ConnectionEvent == DatabaseConnectionEvent.ConnectionOpened)
    {
        e.CustomData["setapprole_cookie"] = CallSetAppRole((SqlConnection)e.Connection);
        return;
    }
    if (e.ConnectionEvent == DatabaseConnectionEvent.ConnectionClosing)
    {
        CallUnSetAppRole((SqlConnection) e.Connection, e.CustomData["setapprole_cookie"]);
        return;
    }
}

private object CallSetAppRole(SqlConnection connection)
{
    SqlCommand cmd = new SqlCommand("sp_setapprole");
    cmd.Connection = connection;
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@rolename", "testapprole"));
    cmd.Parameters.Add(new SqlParameter("@password", "test"));

    cmd.Parameters.Add(new SqlParameter("@fCreateCookie", true));
    var cookieParam = new SqlParameter("@cookie", SqlDbType.VarBinary, 8000);
    cookieParam.Direction = ParameterDirection.Output;
    cmd.Parameters.Add(cookieParam);

    cmd.ExecuteNonQuery();

    return cookieParam.Value;
}

private void CallUnSetAppRole(SqlConnection connection, object cookie)
{
    SqlCommand cmd = new SqlCommand("sp_unsetapprole");
    cmd.Connection = connection;
    cmd.CommandType = CommandType.StoredProcedure;

    var cookieParam = new SqlParameter("@cookie", SqlDbType.VarBinary, 8000);
    cookieParam.Value = cookie;
    cmd.Parameters.Add(cookieParam);

    cmd.ExecuteNonQuery();
}
                
        

Requirements

Namespace: TechTalk.Genome

Assembly: TechTalk.Genome (in TechTalk.Genome.dll)

Version: 4.2.11.59

Editions: Professional, Evaluation, Express

See Also

Context Class | TechTalk.Genome Namespace | DataDomain.ConnectionStateChange