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

时间:2015-01-27 06:12:49

标签: c# ado.net dataset enterprise-library enterprise-library-6

我们在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
    ...
}

我们主要处理一些遗留代码以添加一些重试策略。

1 个答案:

答案 0 :(得分:1)

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

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