用ReliableSqlConnection

用ReliableSqlConnection

我正在尝试使用ReliableSqlConnection类(来自瞬态故障处理应用程序块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填充数据表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20390917/

10-09 03:53