我们在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()
,这是没有重试策略的原始调用。但是,如果代码使用DataSet
和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
...
}
我们主要在处理一些旧代码以添加一些重试策略。
最佳答案
好的,我已经找到了解决方案:
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
...
}