日期:2014-05-17 浏览次数:20432 次
//方法一: 使用TryCatchFinally public static List<ClassA> GetListTryCatch() { List<ClassA> list = new List<ClassA>(); string queryString = @"select * from tableA"; OracleConnection connection = null; try { connection = new OracleConnection(connectionString); OracleCommand command = new OracleCommand(); command.Connection = connection; command.CommandText = queryString; connection.Open(); OracleDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ClassA ca = new ClassA(); ca.ItemName = reader.GetString(0); ca.TargetValue = reader.GetDouble(1); list.Add(ca); } } catch (Exception) { throw; } finally { connection.Close(); // connection.Dispose(); // 不知道要不要加这句 } return list; }
//方法二: 使用Using public static List<ClassA> GetListUsing() { List<ClassA> list = new List<ClassA>(); string queryString = @"select * from tableA"; // use using using (OracleConnection connection = new OracleConnection(connectionString)) { OracleCommand command = new OracleCommand(queryString, connection); connection.Open(); using (OracleDataReader reader = command.ExecuteReader()) { // Always call Read before accessing data. while (reader.Read()) { ClassA ca = new ClassA(); ca.ItemName = reader.GetString(0); ca.TargetValue = reader.GetDouble(1); list.Add(ca); } } } return list; }
using (Class1 cls1 = new Class1(), cls2 = new Class1()) { // the code using cls1, cls2 } // call the Dispose on cls1 and cls2
------解决方案--------------------