在"使用"块中,OracleConnection已关闭

Bax*_*ter 1 c# using try-catch exit oraclecommand

在"使用"块中,如果在catch语句中发出System.Environment.Exit(0),则关闭OracleConnection?

例:

        OracleConnection oracleConnection = getOracleConnection();

        using (oracleConnection)
        {

            try
            {

                oracleConnection.Open();
                OracleCommand cmd = getApplicantsCmd(oracleConnection);
                OracleDataReader rdr = cmd.ExecuteReader(); 

                List<Applicant> applicants = new List<Applicant>();
                while (rdr.Read())
                {
                    Applicant applicant = new Applicant();
                    applicant.email = (string)rdr["GOREMAL_EMAIL_ADDRESS"];
                    applicants.Add(applicant);                   
                }

                return applicants;

            }
            catch (Exception ex)
            {
                Console.WriteLine("Failure getting applicant records: " + ex.Message);
                System.Environment.Exit(0);
                return null;
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果在查找记录时抛出异常,我希望执行停止.

有没有更好的方法来处理这个?

Dar*_*rov 5

未关闭与数据库的物理连接.它只是返回到ADO.NET连接池以供重用.每次要执行SQL查询时,ADO.NET都会保留一个连接池,以避免打开与数据库的物理连接.您还应该包装您的in OracleCommandOracleDataReaderin using语句,以确保即使在发生异常时也能正确处理.