Dapper: The ConnectionString property has not been initialized.

Created on 21 Dec 2016  Â·  4Comments  Â·  Source: StackExchange/Dapper

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; }

Most helpful comment

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:

  • Let Dapper buffer these internally for you (the default, buffered: true)
  • Or consume the results yourself before closing and disposing of the connection.

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.

All 4 comments

capture

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:

  • Let Dapper buffer these internally for you (the default, buffered: true)
  • Or consume the results yourself before closing and disposing of the connection.

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:

  • Let Dapper buffer these internally for you (the default, buffered: true)
  • Or consume the results yourself before closing and disposing of the connection.

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CrescentFresh picture CrescentFresh  Â·  4Comments

silkfire picture silkfire  Â·  4Comments

unipeg picture unipeg  Â·  3Comments

amanguptamindtree picture amanguptamindtree  Â·  4Comments

cpx86 picture cpx86  Â·  4Comments