gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:45:13 24 4
gpt4 key购买 nike

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

例如,

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

关于c# - 如何在 SqlDataAdapter/DataSet 上应用 ReliableSqlConnection 的重试策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28164297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com