Skip to content
Advertisement

What is the proper way of selecting multiple scalars within the same “using” statement with Dapper?

I have wrote the following method, which I hoped to make the method run faster by executing the tasks in parallel:

Unfortunately, I receive the following exception:

System.InvalidOperationException: ‘BeginExecuteReader requires an open and available Connection. The connection’s current state is connecting.’

How should I implement it correctly?

Advertisement

Answer

First things first, executing many commands in parallel against a database will not necessarily be faster, because you put more load on the server and make it try to serve everything at once, which can just cause each query to block the other.

It’s also very difficult to do so on a single connection (unless you use MARS, itself inadvisable), so you would need multiple connections.

In any case, you shouldn’t do multiple queries here at all. You can get all the information in one shot with one query, and the performance will be the same as executing just one of them. This is because they all select from the same table.

Note also that the catch block is redundant, and that you can use Dapper to retrieve multiple columns directly into your object.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement