Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice to fill a DataSet or DataTable asynchronously in ASP.NET?

Given the following code, I have a few questions about best practices:

string connectionString = @"Server=(local)\sqlexpress; Database=master; Integrated Security=true;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from information_schema.columns", connection))
    {
        await connection.OpenAsync();

        DataTable dataTable = new DataTable();
        await Task.Run(() => dataAdapter.Fill(dataTable));
        return dataTable;
    }
}

I've seen several examples that wrap the whole block of code in a Task.Run() call, but I'm not sure if that's better than calling Task.Run() only for the DataAdapter.Fill() method, which feels more flexible and specific (only using await on async tasks).

Is the approach of calling Task.Run() on the Fill() method better than wrapping the whole code block?

Are there any negative side-effects to calling Fill() in Task.Run()? I'm thinking of something along the lines of losing call stack and/or exception information if Fill() has an error.

Is there a better way to write this in ASP.NET?

like image 531
Adrian Anttila Avatar asked Aug 28 '14 22:08

Adrian Anttila


People also ask

Which is better DataSet or DataTable?

If we are going to fetch data from a single database table then DataTable is better choice. While DataSet on the other hand can define DataRelations - which define the relationship between DataTables, much like a foreign key relationship can be set up between tables in a database.

Which method is used to fill data in table?

The Fill method of the DataAdapter is used to populate a DataSet with the results of the SelectCommand of the DataAdapter . Fill takes as its arguments a DataSet to be populated, and a DataTable object, or the name of the DataTable to be filled with the rows returned from the SelectCommand .

Which is faster Sqldataadapter and Sqldatareader?

DataAdapter is used to execute SQL statements and is used to populate the results of SQL Query into a DataSet or DataTable. DataAdapter gets all the rows of the executed SQL statement at once and populates into DataSet or DataTable in memory and hence DataAdapter is bit slower compared to DataReader.

Which is faster DataReader or DataSet?

DataReader provides faster performance, but has read-only and forward-only access. DataSet, on the other hand, is high resource-consuming, but offers more control and a disconnected nature.


1 Answers

In ASP.NET it almost never helps to use Task.Run. What exactly would it improve? It only introduces overhead.

That said, Fill will perform IO (draining the data reader) so you might want to call it asynchronously. Unfortunately, there is no async version of this method.

If you insist on using async IO (which is questionable for database access) you need to find an alternative. Maybe async Entity Framework or raw ADO.NET can help you.

like image 119
usr Avatar answered Sep 20 '22 15:09

usr