المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : using statements and IDisposable



C# Programming
07-15-2009, 01:40 AM
I'm looking at some code like this (sorry for the formatting):

public static OracleDataReader GetDynamicQueryReader (
OracleConnection con,
string sql,
out string msg)
{

sql = Regex.Replace(sql, @"\s+", " ");
try
{
using(OracleCommand cmd = new OracleCommand(sql, con))
{
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
msg = "Successful";
return dr;
}
}
catch (Exception ex)
{
msg = ex.Message;
con.Close();
return null;
}
}

This little chunk (not mine) is called several thousand times, and from the profiler I am using it is the generation of a new OracleCommand object costing the most. And this object can have its parameters set after instantiation.

Normally I would make this a static, but since I am supposed to instantiate it with a using statement to deal with the disposal of unmanaged references, Im not sure I can. Can I? If I determine that there are no memory leaks with a profiler by creating and disposing some unmanaged reference, can I safely make this static? Or should I really just put up with the poor performance?