简体   繁体   中英

Does QueryAsync calls OpenAsync with Dapper?

I have come upon a bit of code that looks like this (it is based of Dapper tutorial):

await using var con = new SqlConnection("some connection");
if (CancellationToken.IsCancellationRequested) await con.OpenAsync(CancellationToken);
return await con.QueryAsync(query, parameters);

How I am understanding this is if Cancellation token was set then it will open an async connection with that token. However if it is not set then it will not try to open a connection and instead it will just call the query statement.

What I'm trying to understand is if you don't call con.OpenAsync will con.QueryAsync call it? Essentially do I need to explicitly call it if there is no cancellation token?

What I'm trying to understand is if you don't call con.OpenAsync will con.QueryAsync call it?

yes

Essentially do I need to explicitly call it if there is no cancellation token?

no; you don't need to call it with or without a cancellation token

However! if you are going to perform multiple related operations - perhaps involving a temporary table or transaction - then you will need to do your own connection state management, ie you will need to call Open[Async] yourself (and Close , if you aren't disposing it immediately).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM