0

Could someone explain to me why this is asynchronous in SQL, but not in OleDb?

And maybe how to fix it please.

SQL

static void Main(string[] args)
{
    var task = Run();
    while(!task.IsCompleted)
        Console.WriteLine("Hmm");
    task.Wait();
}

private static async Task Run()
{
    string conString = @" ... ";
    var con = new SqlConnection(conString);
    con.Open();
    using(var command = new SqlCommand("SELECT * FROM Products2;", con))
    {
        command.Parameters.Add(new SqlParameter("p", 337));
        using(var reader = await command.ExecuteReaderAsync())
            while(await reader.ReadAsync())
                Console.WriteLine(reader.GetString(2));
    }
}

ACCESS

    static void Main(string[] args)
    {
        var task = Run();
        while(!task.IsCompleted)
            Console.WriteLine("Hmm");
        task.Wait();
    }

    private static async Task Run()
    {
        string conString = @" ... ";
        var con = new OleDbConnection(conString);
        con.Open();
        using(var command = new OleDbCommand("SELECT * FROM Products2;", con))
        {
            command.Parameters.Add(new OleDbParameter("p", 337));
            using(var reader = await command.ExecuteReaderAsync())
                while(await reader.ReadAsync()) //Note - Calling Async
                    Console.WriteLine(reader.GetString(2));
        }
    }

Just so you don't have to copy this in yourself, during execution of SQL, it will print the result and "Hmm" side by side. In OleDb is just prints the result.


I can fix it by wrapping await Task.Run(() => Console.WriteLine(reader.GetString(2)));, but that just produces other errors.


Result from SQL

Result from SQL

12
  • 1
    You say "how to fix it". What needs to be fixed?
    – Sean Lange
    Commented Jul 24, 2017 at 13:42
  • None of these are awaited, and therefore none of these should be async. They will both run on the same thread
    – Sasha
    Commented Jul 24, 2017 at 13:42
  • @Jaxi, You are welcome to try it yourself. Commented Jul 24, 2017 at 13:44
  • @SeanLange, Sorry, I would like to make OleDb asynchronous. Commented Jul 24, 2017 at 13:45
  • 1
    I would but I dont have an environment to test it on at the moment :) but where you do while !task.iscompleted blocks the console thread writing hmm
    – Sasha
    Commented Jul 24, 2017 at 13:45

1 Answer 1

1

Could someone explain to me why this is asynchronous in SQL, but not in OleDb?

It's up to the ADO.NET provider whether it implements asynchronous methods or not. If a provider doesn't support asynchrony, then the asynchronous methods just run synchronously.

And maybe how to fix it please.

If your real application is on ASP.NET, just continue to call them asynchronously; they'll run synchronously, but there isn't anything you can do about it, and if they're upgraded in the future to support asynchrony, your code will automagically use that new capability.

If your real application is a UI app, then you will need to wrap your database code inside a background thread (e.g., Task.Run). It's up to you whether to use asynchronous or synchronous APIs in that case; either way, I'd be sure to leave a comment explaining that the ADO.NET provider executes synchronously and that's why it's in a background thread.

3
  • Thank you. It feels counterintuitive to not just throw unimplenemted exceptions. I have a oledb adapter that I might be able to hide some async calls in, since I dont wsnt Task.Run where sql also executes. It is a UI app umforunately. :) Commented Jul 24, 2017 at 14:22
  • Do you kbow of any documentation verifying that tjis specific provider does not implement async? Just for bonus points ;) Commented Jul 24, 2017 at 14:23
  • @ChrisWohlert: No, sorry. I just know how the architecture works, not the specific implementations. SQL is for-sure async-compatible, and they decided not to upgrade the Oracle one, and SQLite isn't async either. In your case, I'm not sure whether it's the OleDb-for-ADO.NET or the Access-for-OleDb component that is dropping the async support. Commented Jul 24, 2017 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.