Below is my method, when it is running, I debug found the result had values before connection released, but after connection release, result will thow an exception as "The ConnectionString property has not been initialized."
public IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = CommandType.Text)
{
IEnumerable<T> result;
using (IDbConnection con = ConnectionFactory.Instance.GetConnection())
{
con.Open();
result = con.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType);
con.Close();
}
return result;
}
I see that you have buffered
set to false
. If you do this and immediately close and dispose the connection, you're doing so before the results are read. You have 2 choices here:
buffered: true
)The current code has the effect of shutting the door while the results are still walking in, so that'll almost always blow up. The only way it'll succeed is if the query was so far and the result set so small as to finish before the .Close()
call gets hit.
Also note: Dapper will .Open()
and .Close()
the connection for you, you can simply remove these calls. But the dispose (from the using
) itself is still an issue in your illustration.
Ok. Got it. Thanks Nick.
On Wed, Dec 21, 2016 at 8:31 PM +0800, "Nick Craver" <[email protected]notifications@github.com> wrote:
I see that you have buffered set to false. If you do this and immediately close and dispose the connection, you're doing so before the results are read. You have 2 choices here:
The current code has the effect of shutting the door while the results are still walking in, so that'll almost always blow up. The only way it'll succeed is if the query was so far and the result set so small as to finish before the .Close() call gets hit.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/StackExchange/dapper-dot-net/issues/664#issuecomment-268511739, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AIUB8OhN9Sq1RafkcTuxuIP6qGBrhhQjks5rKRwggaJpZM4LSxf1.
@lasikye anything else I can help with here, or want me to close this one out?
Most helpful comment
I see that you have
buffered
set tofalse
. If you do this and immediately close and dispose the connection, you're doing so before the results are read. You have 2 choices here:buffered: true
)The current code has the effect of shutting the door while the results are still walking in, so that'll almost always blow up. The only way it'll succeed is if the query was so far and the result set so small as to finish before the
.Close()
call gets hit.Also note: Dapper will
.Open()
and.Close()
the connection for you, you can simply remove these calls. But the dispose (from theusing
) itself is still an issue in your illustration.