【问题标题】:Fill a DataTable with ReliableSqlConnection使用 ReliableSqlConnection 填充数据表
【发布时间】:2013-12-21 20:34:45
【问题描述】:

我正在尝试使用 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 作为我的连接重试?

从文档中并不完全清楚这一切是如何在幕后工作的。

【问题讨论】:

标签: c# gridview ado.net datatable


【解决方案1】:

也许这会起作用?还没测试。

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

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

编辑我的代码的 sn-p:

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

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

Azure SQL 需要这个

【讨论】:

  • 如果我们只将“da.Fill(dt)”包裹在 ExecuteAction 中,那行得通吗?
【解决方案2】:

也许这会起作用?还没测试。

DataTable GetTable(string commandText)
{
    DataTable DataTable = null;
    DataTable TempDataTable = null;
    try
    {
        TempDataTable = new DataTable();
        TempDataTable.Locale = CultureInfo.InvariantCulture;

        using (ReliableSqlConnection ReliableSqlConnection = new ReliableSqlConnection(ConnectionString))
        {
            ReliableSqlConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand(commandText, ReliableSqlConnection.Current))
            {
                SqlCommand.CommandType = CommandType.Text;
                TempDataTable.Load(SqlCommand.ExecuteReaderWithRetry());
            }
        }
        DataTable = TempDataTable;
        TempDataTable = null;
    }
    finally
    {
        if (TempDataTable != null)
        {
            TempDataTable.Dispose();
        }
    }
    return DataTable;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 2012-05-19
    • 2018-12-18
    相关资源
    最近更新 更多