예제 #1
0
        /// <summary>
        /// Finds the database objects determined by the query string and parameters passed using Oracle database.
        /// </summary>
        /// <param name="queryString">The query to be executed</param>
        /// <exception cref="DatabaseException">If an error occurs when trying to retrieve the data from the database.</exception>
        /// <returns>The list of objects populated with the database data.</returns>
        protected List <M> FindByQueryOracle(string queryString, params QueryParameter[] parameters)
        {
            var objectList = new List <M>();

            try
            {
                using (OracleConnection cnn = new OracleConnection(cnnStr))
                {
                    using (OracleCommand cmd = new OracleCommand(queryString, cnn))
                    {
                        foreach (QueryParameter parameter in parameters)
                        {
                            DatabaseUtils.AddCommandParameter(cmd, parameter);
                        }

                        cnn.Open();

                        using (OracleDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    objectList.Add(CreateObjectFromDataReader(reader));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new DatabaseException("An error has occurred when searching for records on a SQL Server database.", ex);
            }

            return(objectList);
        }
예제 #2
0
        /// <summary>
        /// Executes the given procedure on the SQL Server database
        /// </summary>
        /// <param name="procedureName">The procedure to be executed.</param>
        /// <param name="parameters">The parameters to be used.</param>
        protected void ExecuteSOracleProcedure(string procedureName, params QueryParameter[] parameters)
        {
            try
            {
                using (OracleConnection cnn = new OracleConnection(cnnStr))
                {
                    using (OracleCommand cmd = cnn.CreateCommand())
                    {
                        cmd.CommandText = procedureName;
                        cmd.CommandType = CommandType.StoredProcedure;

                        // Set the parameters into the stored procedure.
                        foreach (QueryParameter parameter in parameters)
                        {
                            DatabaseUtils.AddCommandParameter(cmd, parameter);
                        }

                        cnn.Open();
                        cmd.ExecuteNonQuery();

                        // Retrieve the parameter values.
                        foreach (QueryParameter parameter in parameters)
                        {
                            if (parameter.IsInputOutput() || parameter.IsOutput())
                            {
                                parameter.Value = cmd.Parameters[parameter.Name].Value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new DatabaseException(string.Format("An error has occurred when executing the procedure {0} on a SQL Server database.", procedureName), ex);
            }
        }