// <fileinfo name="OrderDetailsCollection_Base.cs">
//      <copyright>
//          All rights reserved.
//      </copyright>
//      <remarks>
//          Do not change this source code manually. Changes to this file may 
//          cause incorrect behavior and will be lost if the code is regenerated.
//      </remarks>
//      <generator rewritefile="True" infourl="http://www.SharpPower.com">RapTier</generator>
// </fileinfo>

using System;
using System.Data;

namespace MyCompany.MyProject.Db
{
    /// <summary>
    /// The base class for <see cref="OrderDetailsCollection"/>. Provides methods 
    /// for common database table operations. 
    /// </summary>
    /// <remarks>
    /// Do not change this source code. Update the <see cref="OrderDetailsCollection"/>
    /// class if you need to add or change some functionality.
    /// </remarks>
    public abstract class OrderDetailsCollection_Base
    {
        // Constants
        public const string OrderIDColumnName = "OrderID";
        public const string ProductIDColumnName = "ProductID";
        public const string UnitPriceColumnName = "UnitPrice";
        public const string QuantityColumnName = "Quantity";
        public const string DiscountColumnName = "Discount";

        // Instance fields
        private Northwind _db;

        /// <summary>
        /// Initializes a new instance of the <see cref="OrderDetailsCollection_Base"/> 
        /// class with the specified <see cref="Northwind"/>.
        /// </summary>
        /// <param name="db">The <see cref="Northwind"/> object.</param>
        public OrderDetailsCollection_Base(Northwind db)
        {
            _db = db;
        }

        /// <summary>
        /// Gets the database object that this table belongs to.
        /// </summary>
        /// <value>The <see cref="Northwind"/> object.</value>
        protected Northwind Database
        {
            get { return _db; }
        }

        /// <summary>
        /// Gets an array of all records from the <c>Order Details</c> table.
        /// </summary>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        public virtual OrderDetailsRow[] GetAll()
        {
            return MapRecords(CreateGetAllCommand());
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object that 
        /// includes all records from the <c>Order Details</c> table.
        /// </summary>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetAllAsDataTable()
        {
            return MapRecordsToDataTable(CreateGetAllCommand());
        }

        /// <summary>
        /// Creates and returns an <see cref="System.Data.IDbCommand"/> object that is used
        /// to retrieve all records from the <c>Order Details</c> table.
        /// </summary>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetAllCommand()
        {
            return _db.CreateCommand("dbo._OrderDetails_GetAll", true);
        }

        /// <summary>
        /// Gets the first <see cref="OrderDetailsRow"/> objects that 
        /// match the search condition.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: 
        /// <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <returns>An instance of <see cref="OrderDetailsRow"/> or null reference 
        /// (Nothing in Visual Basic) if the object was not found.</returns>
        public OrderDetailsRow GetRow(string whereSql)
        {
            int totalRecordCount = -1;
            OrderDetailsRow[] rows = GetAsArray(whereSql, null, 0, 1, ref totalRecordCount);
            return 0 == rows.Length ? null : rows[0];
        }

        /// <summary>
        /// Gets an array of <see cref="OrderDetailsRow"/> objects that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: 
        /// <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: <c>"LastName ASC, FirstName ASC"</c>.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        public OrderDetailsRow[] GetAsArray(string whereSql, string orderBySql)
        {
            int totalRecordCount = -1;
            return GetAsArray(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
        }

        /// <summary>
        /// Gets an array of <see cref="OrderDetailsRow"/> objects that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example:
        /// <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: <c>"LastName ASC, FirstName ASC"</c>.</param>
        /// <param name="startIndex">The index of the first record to return.</param>
        /// <param name="length">The number of records to return.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        public virtual OrderDetailsRow[] GetAsArray(string whereSql, string orderBySql,
                            int startIndex, int length, ref int totalRecordCount)
        {
            using(IDataReader reader = _db.ExecuteReader(CreateGetCommand(whereSql, orderBySql)))
            {
                return MapRecords(reader, startIndex, length, ref totalRecordCount);
            }
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object filled with data that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: "FirstName='Smith' AND Zip=75038".</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: "LastName ASC, FirstName ASC".</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public DataTable GetAsDataTable(string whereSql, string orderBySql)
        {
            int totalRecordCount = -1;
            return GetAsDataTable(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object filled with data that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: "FirstName='Smith' AND Zip=75038".</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: "LastName ASC, FirstName ASC".</param>
        /// <param name="startIndex">The index of the first record to return.</param>
        /// <param name="length">The number of records to return.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetAsDataTable(string whereSql, string orderBySql,
                            int startIndex, int length, ref int totalRecordCount)
        {
            using(IDataReader reader = _db.ExecuteReader(CreateGetCommand(whereSql, orderBySql)))
            {
                return MapRecordsToDataTable(reader, startIndex, length, ref totalRecordCount);
            }
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object for the specified search criteria.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: "FirstName='Smith' AND Zip=75038".</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: "LastName ASC, FirstName ASC".</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetCommand(string whereSql, string orderBySql)
        {
            string sql = "SELECT * FROM [dbo].[Order Details]";
            if(null != whereSql && 0 < whereSql.Length)
                sql += " WHERE " + whereSql;
            if(null != orderBySql && 0 < orderBySql.Length)
                sql += " ORDER BY " + orderBySql;
            return _db.CreateCommand(sql);
        }

        /// <summary>
        /// Gets <see cref="OrderDetailsRow"/> by the primary key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>An instance of <see cref="OrderDetailsRow"/> or null reference 
        /// (Nothing in Visual Basic) if the object was not found.</returns>
        public virtual OrderDetailsRow GetByPrimaryKey(int orderID, int productID)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_GetByPrimaryKey", true);
            AddParameter(cmd, "OrderID", orderID);
            AddParameter(cmd, "ProductID", productID);
            OrderDetailsRow[] tempArray = MapRecords(cmd);
            return 0 == tempArray.Length ? null : tempArray[0];
        }

        /// <summary>
        /// Gets an array of <see cref="OrderDetailsRow"/> objects 
        /// by the <c>FK_Order_Details_Orders</c> foreign key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        public virtual OrderDetailsRow[] GetByOrderID(int orderID)
        {
            return MapRecords(CreateGetByOrderIDCommand(orderID));
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object 
        /// by the <c>FK_Order_Details_Orders</c> foreign key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetByOrderIDAsDataTable(int orderID)
        {
            return MapRecordsToDataTable(CreateGetByOrderIDCommand(orderID));
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to 
        /// return records by the <c>FK_Order_Details_Orders</c> foreign key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetByOrderIDCommand(int orderID)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_GetBy_OrderID", true);
            AddParameter(cmd, "OrderID", orderID);
            return cmd;
        }

        /// <summary>
        /// Gets an array of <see cref="OrderDetailsRow"/> objects 
        /// by the <c>FK_Order_Details_Products</c> foreign key.
        /// </summary>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        public virtual OrderDetailsRow[] GetByProductID(int productID)
        {
            return MapRecords(CreateGetByProductIDCommand(productID));
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object 
        /// by the <c>FK_Order_Details_Products</c> foreign key.
        /// </summary>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetByProductIDAsDataTable(int productID)
        {
            return MapRecordsToDataTable(CreateGetByProductIDCommand(productID));
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to 
        /// return records by the <c>FK_Order_Details_Products</c> foreign key.
        /// </summary>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetByProductIDCommand(int productID)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_GetBy_ProductID", true);
            AddParameter(cmd, "ProductID", productID);
            return cmd;
        }

        /// <summary>
        /// Adds a new record into the <c>Order Details</c> table.
        /// </summary>
        /// <param name="value">The <see cref="OrderDetailsRow"/> object to be inserted.</param>
        public virtual void Insert(OrderDetailsRow value)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_Insert", true);
            AddParameter(cmd, "OrderID", value.OrderID);
            AddParameter(cmd, "ProductID", value.ProductID);
            AddParameter(cmd, "UnitPrice", value.UnitPrice);
            AddParameter(cmd, "Quantity", value.Quantity);
            AddParameter(cmd, "Discount", value.Discount);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Updates a record in the <c>Order Details</c> table.
        /// </summary>
        /// <param name="value">The <see cref="OrderDetailsRow"/>
        /// object used to update the table record.</param>
        /// <returns>true if the record was updated; otherwise, false.</returns>
        public virtual bool Update(OrderDetailsRow value)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_Update", true);
            AddParameter(cmd, "UnitPrice", value.UnitPrice);
            AddParameter(cmd, "Quantity", value.Quantity);
            AddParameter(cmd, "Discount", value.Discount);
            AddParameter(cmd, "OrderID", value.OrderID);
            AddParameter(cmd, "ProductID", value.ProductID);
            return 0 != cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Updates the <c>Order Details</c> table and calls the <c>AcceptChanges</c> method
        /// on the changed DataRow objects.
        /// </summary>
        /// <param name="table">The <see cref="System.Data.DataTable"/> used to update the data source.</param>
        public void Update(DataTable table)
        {
            Update(table, true);
        }

        /// <summary>
        /// Updates the <c>Order Details</c> table. Pass <c>false</c> as the <c>acceptChanges</c> 
        /// argument when your code calls this method in an ADO.NET transaction context. Note that in 
        /// this case, after you call the Update method you need call either <c>AcceptChanges</c> 
        /// or <c>RejectChanges</c> method on the DataTable object.
        /// <code>
        /// MyDb db = new MyDb();
        /// try
        /// {
        ///        db.BeginTransaction();
        ///        db.MyCollection.Update(myDataTable, false);
        ///        db.CommitTransaction();
        ///        myDataTable.AcceptChanges();
        /// }
        /// catch(Exception)
        /// {
        ///        db.RollbackTransaction();
        ///        myDataTable.RejectChanges();
        /// }
        /// </code>
        /// </summary>
        /// <param name="table">The <see cref="System.Data.DataTable"/> used to update the data source.</param>
        /// <param name="acceptChanges">Specifies whether this method calls the <c>AcceptChanges</c>
        /// method on the changed DataRow objects.</param>
        public virtual void Update(DataTable table, bool acceptChanges)
        {
            DataRowCollection rows = table.Rows;
            for(int i = rows.Count - 1; i >= 0; i--)
            {
                DataRow row = rows[i];
                switch(row.RowState)
                {
                    case DataRowState.Added:
                        Insert(MapRow(row));
                        if(acceptChanges)
                            row.AcceptChanges();
                        break;

                    case DataRowState.Deleted:
                        // Temporary reject changes to be able to access to the PK column(s)
                        row.RejectChanges();
                        try
                        {
                            DeleteByPrimaryKey((int)row["OrderID"], (int)row["ProductID"]);
                        }
                        finally
                        {
                            row.Delete();
                        }
                        if(acceptChanges)
                            row.AcceptChanges();
                        break;
                        
                    case DataRowState.Modified:
                        Update(MapRow(row));
                        if(acceptChanges)
                            row.AcceptChanges();
                        break;
                }
            }
        }

        /// <summary>
        /// Deletes the specified object from the <c>Order Details</c> table.
        /// </summary>
        /// <param name="value">The <see cref="OrderDetailsRow"/> object to delete.</param>
        /// <returns>true if the record was deleted; otherwise, false.</returns>
        public bool Delete(OrderDetailsRow value)
        {
            return DeleteByPrimaryKey(value.OrderID, value.ProductID);
        }

        /// <summary>
        /// Deletes a record from the <c>Order Details</c> table using
        /// the specified primary key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>true if the record was deleted; otherwise, false.</returns>
        public virtual bool DeleteByPrimaryKey(int orderID, int productID)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_DeleteByPrimaryKey", true);
            AddParameter(cmd, "OrderID", orderID);
            AddParameter(cmd, "ProductID", productID);
            return 0 < cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Deletes records from the <c>Order Details</c> table using the 
        /// <c>FK_Order_Details_Orders</c> foreign key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <returns>The number of records deleted from the table.</returns>
        public int DeleteByOrderID(int orderID)
        {
            return CreateDeleteByOrderIDCommand(orderID).ExecuteNonQuery();
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
        /// delete records using the <c>FK_Order_Details_Orders</c> foreign key.
        /// </summary>
        /// <param name="orderID">The <c>OrderID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateDeleteByOrderIDCommand(int orderID)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_DeleteBy_OrderID", true);
            AddParameter(cmd, "OrderID", orderID);
            return cmd;
        }

        /// <summary>
        /// Deletes records from the <c>Order Details</c> table using the 
        /// <c>FK_Order_Details_Products</c> foreign key.
        /// </summary>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>The number of records deleted from the table.</returns>
        public int DeleteByProductID(int productID)
        {
            return CreateDeleteByProductIDCommand(productID).ExecuteNonQuery();
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
        /// delete records using the <c>FK_Order_Details_Products</c> foreign key.
        /// </summary>
        /// <param name="productID">The <c>ProductID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateDeleteByProductIDCommand(int productID)
        {
            IDbCommand cmd = _db.CreateCommand("dbo._OrderDetails_DeleteBy_ProductID", true);
            AddParameter(cmd, "ProductID", productID);
            return cmd;
        }

        /// <summary>
        /// Deletes <c>Order Details</c> records that match the specified criteria.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. 
        /// For example: <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <returns>The number of deleted records.</returns>
        public int Delete(string whereSql)
        {
            return CreateDeleteCommand(whereSql).ExecuteNonQuery();
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used 
        /// to delete <c>Order Details</c> records that match the specified criteria.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. 
        /// For example: <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateDeleteCommand(string whereSql)
        {
            string sql = "DELETE FROM [dbo].[Order Details]";
            if(null != whereSql && 0 < whereSql.Length)
                sql += " WHERE " + whereSql;
            return _db.CreateCommand(sql);
        }

        /// <summary>
        /// Deletes all records from the <c>Order Details</c> table.
        /// </summary>
        /// <returns>The number of deleted records.</returns>
        public int DeleteAll()
        {
            return _db.CreateCommand("dbo._OrderDetails_DeleteAll", true).ExecuteNonQuery();
        }

        /// <summary>
        /// Reads data using the specified command and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="command">The <see cref="System.Data.IDbCommand"/> object.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        protected OrderDetailsRow[] MapRecords(IDbCommand command)
        {
            using(IDataReader reader = _db.ExecuteReader(command))
            {
                return MapRecords(reader);
            }
        }

        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        protected OrderDetailsRow[] MapRecords(IDataReader reader)
        {
            int totalRecordCount = -1;
            return MapRecords(reader, 0, int.MaxValue, ref totalRecordCount);
        }

        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <param name="startIndex">The index of the first record to map.</param>
        /// <param name="length">The number of records to map.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>An array of <see cref="OrderDetailsRow"/> objects.</returns>
        protected virtual OrderDetailsRow[] MapRecords(IDataReader reader, 
                                        int startIndex, int length, ref int totalRecordCount)
        {
            if(0 > startIndex)
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "StartIndex cannot be less than zero.");
            if(0 > length)
                throw new ArgumentOutOfRangeException("length", length, "Length cannot be less than zero.");

            int orderIDColumnIndex = reader.GetOrdinal("OrderID");
            int productIDColumnIndex = reader.GetOrdinal("ProductID");
            int unitPriceColumnIndex = reader.GetOrdinal("UnitPrice");
            int quantityColumnIndex = reader.GetOrdinal("Quantity");
            int discountColumnIndex = reader.GetOrdinal("Discount");

            System.Collections.ArrayList recordList = new System.Collections.ArrayList();
            int ri = -startIndex;
            while(reader.Read())
            {
                ri++;
                if(ri > 0 && ri <= length)
                {
                    OrderDetailsRow record = new OrderDetailsRow();
                    recordList.Add(record);

                    record.OrderID = Convert.ToInt32(reader.GetValue(orderIDColumnIndex));
                    record.ProductID = Convert.ToInt32(reader.GetValue(productIDColumnIndex));
                    record.UnitPrice = Convert.ToDecimal(reader.GetValue(unitPriceColumnIndex));
                    record.Quantity = Convert.ToInt16(reader.GetValue(quantityColumnIndex));
                    record.Discount = Convert.ToSingle(reader.GetValue(discountColumnIndex));

                    if(ri == length && 0 != totalRecordCount)
                        break;
                }
            }

            totalRecordCount = 0 == totalRecordCount ? ri + startIndex : -1;
            return (OrderDetailsRow[])(recordList.ToArray(typeof(OrderDetailsRow)));
        }

        /// <summary>
        /// Reads data using the specified command and returns 
        /// a filled <see cref="System.Data.DataTable"/> object.
        /// </summary>
        /// <param name="command">The <see cref="System.Data.IDbCommand"/> object.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        protected DataTable MapRecordsToDataTable(IDbCommand command)
        {
            using(IDataReader reader = _db.ExecuteReader(command))
            {
                return MapRecordsToDataTable(reader);
            }
        }

        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// a filled <see cref="System.Data.DataTable"/> object.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        protected DataTable MapRecordsToDataTable(IDataReader reader)
        {
            int totalRecordCount = 0;
            retur