Search code examples
c#ado.netdatasetenterprise-libraryenterprise-library-6

How to apply ReliableSqlConnection's retry policy on SqlDataAdapter/DataSet


We applied Microsoft's Enterprise Library Transient Fault Handling Block on the Azure Sql connection and commands.

For example,

using(var sqlConnection = new ReliableSqlConnection(_connectionString, _connectionRetry, _commandRetry)
{
    var command = sqlConnection.CreateCommand();
    command.CommandText = "...";
    sqlConnection.Open();

    using(var reader = sqlConnection.ExecuteCommand<SqlDataReader>(command))
    {
        ...
    }
}

It would work for such as command.ExecuteReader(), which is the original call without retry policy. But what if the code uses DataSet and SqlDataAdapter:

using(var sqlConnection = new ReliableSqlConnection(_connectionString, _connectionRetry, _commandRetry)
{
    var command = sqlConnection.CreateCommand();
    command.CommandText = "...";
    sqlConnection.Open();

    // how to convert below codes to right way?
    var adapter = new SqlDataAdapter(command);
    var dataSet = new DataSet();
    adapter.Fill(dataSet);

    // handle dataSet
    ...
}

We are mainly working on some legacy code to add some retry strategy.


Solution

  • Ok, I have found a solution for this:

    using(var sqlConnection = new ReliableSqlConnection(_connectionString, _connectionRetry, _commandRetry)
    {
        var command = sqlConnection.CreateCommand();
        command.CommandText = "...";
        sqlConnection.Open();
    
        var dataReader = sqlConnection.ExecuteCommand<SqlDataReader>();
        var dataTable = new DataTable();
        dataTable.Load(dataReader);
    
        // handle dataTable, in our case the data set only returns one table, so it's ok
        ...
    }