Does ExecuteScalar close the connection automatically?
No, you need to explicitly open and close the connection when using ExecuteScalar().
You could create an overload using an extension method though I'm not sure if it's a good idea.
public static object ExecuteScalar(this IDbCommand Command, bool CloseConnetion)
{
(if Command == null)
throw new NullReferenceException();
object obj = null;
try
{
obj = Command.ExecuteScalar();
}
finally
{
if(CloseConnection && Command.Connection.State != ConnectionState.Closed)
Command.Connection.Close();
}
return obj;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With