gpt4 book ai didi

c# - 用 ReliableSqlConnection 填充 DataTable

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:36 25 4
gpt4 key购买 nike

我正在尝试使用 ReliableSqlConnection 类(来自 transient 故障处理应用程序 block NuGet 包)。

我的最终结果是我想使用可靠的重试机制填充 GridView。

我的代码是这样的:

        RetryStrategy retryStrategy = new Incremental("incremental", 5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
RetryPolicy retryPolicy = new RetryPolicy<SqlDatabaseTransientErrorDetectionStrategy>(retryStrategy);

IList<RetryStrategy> strategies = new List<RetryStrategy> { retryStrategy };
RetryManager manager = new RetryManager(strategies, "incremental");
RetryManager.SetDefault(manager);

using (ReliableSqlConnection connection =
new ReliableSqlConnection(
ConfigurationManager.ConnectionStrings["AdventureWorksLTConnectionString"].ConnectionString,
retryPolicy))
{
SqlCommand command = new SqlCommand(
"SELECT top 5 [FirstName], [LastName], [CompanyName], [EmailAddress] FROM [SalesLT].[Customer]",
connection.Current);
command.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(command);

DataTable table = new DataTable();

adapter.Fill(table);

GridView1.DataSource = table.DefaultView;
GridView1.DataBind();
}

这里的Fill方法内部是否使用了ExecuteReaderWithRetry方法?有必要吗?还是仅通过使用 ReliableSqlConnection 作为我的连接自动设置为重试?

从文档中并不能完全清楚这一切到底是如何工作的。

最佳答案

也许这会奏效?还没有测试过。

retryPolicy.ExecuteAction(() =>
{
adapter.Fill(table);
});

找到 https://stackoverflow.com/questions/18268005/stored-procedure-populate-deleted-table-entities-back-to-dataset

编辑我的代码片段:

retryPolicy.ExecuteAction(() =>
{
using (SqlConnection con = new SqlConnection(GetConStr()))
{
com.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(com);

con.OpenWithRetry();
da.Fill(dt);
}
});

我需要这个用于 Azure SQL

关于c# - 用 ReliableSqlConnection 填充 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390917/

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