如何在 SqlDataAdapter/DataSet 上应用 ReliableSqlConnection 的重试策略

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

我们在 Azure Sql 连接和命令上应用了 Microsoft 的企业库 Transient Fault Handling Block

例如,

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

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

它适用于 command.ExecuteReader(),这是没有重试策略的原始调用。但是如果代码使用 DataSetSqlDataAdapter:

怎么办
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
    ...
}

我们主要致力于一些遗留代码以添加一些重试策略。

好的,我找到了解决方案:

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
    ...
}