예제 #1
0
        /// <summary>
        /// Function to Load a CampaignEntity from database.
        /// </summary>
        /// <param name="propertyName">A string with the name of the field or a
        /// constant from the class that represent that field</param>
        /// <param name="expValue">The value that will be inserted on the where
        /// clause of the sql query</param>
        /// <param name="loadRelation">If is true load the relations</param>
        /// <returns>A list containing all the entities that match the where clause</returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="propertyName"/> is null or empty.
        /// If <paramref name="propertyName"/> is not a property of CampaignEntity class.
        /// If <paramref name="expValue"/> is null.
        /// </exception>
        /// <exception cref="UtnEmallDataAccessException">
        /// If an DbException occurs in the try block while accessing the database.
        /// </exception>
        public Collection <CampaignEntity> LoadWhere(string propertyName, object expValue, bool loadRelation, OperatorType operatorType)
        {
            if (String.IsNullOrEmpty(propertyName) || expValue == null)
            {
                throw new ArgumentException("The argument can not be null or be empty", "propertyName");
            }
            if (!properties.ContainsKey(propertyName))
            {
                throw new ArgumentException("The property " + propertyName + " is not a property of this entity", "propertyName");
            }
            Collection <CampaignEntity> campaignList;

            bool closeConnection = false;

            try
            {
                // Open a new connection with a database if necessary
                if (dbConnection == null || dbConnection.State.CompareTo(ConnectionState.Closed) == 0)
                {
                    closeConnection = true;
                    dbConnection    = dataAccess.GetNewConnection();
                    dbConnection.Open();
                }

                string op = DataAccessConnection.GetOperatorString(operatorType);
                // Build the query string

                string cmdText = "SELECT idCampaign, description, name, startDate, stopDate, idUser, timestamp FROM [Campaign] WHERE " + propertyName + " " + op + " @expValue";
                // Create the command

                IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction);
                // Add parameters values to the command

                IDbDataParameter parameter = dataAccess.GetNewDataParameter();
                parameter.ParameterName = "@expValue";
                Type parameterType = properties[propertyName];
                parameter.DbType = DataAccessConnection.GetParameterDBType(parameterType);

                parameter.Value = expValue;
                sqlCommand.Parameters.Add(parameter);
                // Create a DataReader

                IDataReader reader = sqlCommand.ExecuteReader();
                campaignList = new Collection <CampaignEntity>();
                CampaignEntity campaign;
                List <int>     listId = new List <int>();
                // Add list of Ids to a list
                while (reader.Read())
                {
                    listId.Add(reader.GetInt32(0));
                }
                // Close the reader

                reader.Close();
                // Load the entities

                foreach (int id in listId)
                {
                    campaign = Load(id, loadRelation, null);
                    campaignList.Add(campaign);
                }
            }
            catch (DbException dbException)
            {
                // Catch DbException and rethrow as custom exception
                throw new UtnEmallDataAccessException(dbException.Message, dbException);
            }
            finally
            {
                // Close connection if it was opened by myself
                if (closeConnection)
                {
                    dbConnection.Close();
                }
            }
            return(campaignList);
        }