ExecuteScalar 会自动关闭连接吗?
问问题
3535 次
3 回答
6
不,您需要在使用 ExecuteScalar() 时显式打开和关闭连接。
于 2010-07-12T14:55:01.400 回答
5
您可以使用扩展方法创建重载,但我不确定这是否是个好主意。
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;
}
于 2010-07-12T15:11:57.490 回答
3
那要看。
可以编写一个实现来IDbCommand
关闭连接。
但据我所知,提供的实现不会关闭连接。
于 2010-07-12T14:51:10.950 回答