悬赏园豆:10
[已关闭问题]
<P>无意间在<A class="" href="http://www.google.com/codesearch?hl=en&q=+lang:c%23+SQLiteFactory+show:cb5wnwNx4V4:snjGWivFAH8:akJ7Y2rgnmo&sa=N&cd=6&ct=rc&cs_p=http://buktools.googlecode.com/svn&cs_f=trunk/.NET/BukTools/src/Buk.Core/Data/DatabaseHelper.cs" mce_href="http://www.google.com/codesearch?hl=en&q=+lang:c%23+SQLiteFactory+show:cb5wnwNx4V4:snjGWivFAH8:akJ7Y2rgnmo&sa=N&cd=6&ct=rc&cs_p=http://buktools.googlecode.com/svn&cs_f=trunk/.NET/BukTools/src/Buk.Core/Data/DatabaseHelper.cs">google codesearch</A> 上看到这样一段代码:<BR>其中有个ExecuteReader方法,如下:<BR>public DbDataReader ExecuteReader(string query,CommandType commandtype, ConnectionState connectionstate)<BR><SPAN id=l405> {<BR></SPAN><SPAN id=l406> objSelectOrQueryCommand.CommandText = query;<BR></SPAN><SPAN id=l407> objSelectOrQueryCommand.CommandType = commandtype;<BR></SPAN><SPAN id=l408> DbDataReader reader=null;<BR></SPAN><SPAN id=l409> try<BR></SPAN><SPAN id=l410> {<BR></SPAN><SPAN id=l411> if (objConnection.State == System.Data.ConnectionState.Closed)<BR></SPAN><SPAN id=l412> {<BR></SPAN><SPAN id=l413> objConnection.Open();<BR></SPAN><SPAN id=l414> }<BR></SPAN><SPAN id=l415> if (connectionstate == ConnectionState.CloseOnExit)<BR></SPAN><SPAN id=l416> {<BR></SPAN><SPAN id=l417> reader = objSelectOrQueryCommand.ExecuteReader(CommandBehavior.CloseConnection);<BR></SPAN><SPAN id=l418> }<BR></SPAN><SPAN id=l419> else<BR></SPAN><SPAN id=l420> &nb
问题补充:
{
reader = objSelectOrQueryCommand.ExecuteReader();
}
}
catch (Exception ex)
{
HandleExceptions(ex);
}
finally
{
objSelectOrQueryCommand.Parameters.Clear();
}
return reader;
}
HandleExceptions方法如下:(此异常处理中,如果LogErrors,HandleErrors为true的话,DataReader的连接不是就没有机会关闭了吗?会不会造成链接泄漏??)
private void HandleExceptions(Exception ex)
{
string exMsg = ex.Message + Environment.NewLine + ex.StackTrace;
if (LogErrors)
{
WriteToLog(exMsg);
}
if (HandleErrors)
{
strLastError = exMsg;
}
else
{
throw ex;
}
}
钱小柜
|
初学一级
|
园豆:
190
提问于:2008-01-23 17:55