// <fileinfo name="AlphabeticallistofproductsCollection_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="AlphabeticallistofproductsCollection"/>. Provides methods
/// for common database view operations.
/// </summary>
/// <remarks>
/// Do not change this source code. Update the <see cref="AlphabeticallistofproductsCollection"/>
/// class if you need to add or change some functionality.
/// </remarks>
public abstract class AlphabeticallistofproductsCollection_Base
{
// Constants
public const string ProductIDColumnName = "ProductID";
public const string ProductNameColumnName = "ProductName";
public const string SupplierIDColumnName = "SupplierID";
public const string CategoryIDColumnName = "CategoryID";
public const string QuantityPerUnitColumnName = "QuantityPerUnit";
public const string UnitPriceColumnName = "UnitPrice";
public const string UnitsInStockColumnName = "UnitsInStock";
public const string UnitsOnOrderColumnName = "UnitsOnOrder";
public const string ReorderLevelColumnName = "ReorderLevel";
public const string DiscontinuedColumnName = "Discontinued";
public const string CategoryNameColumnName = "CategoryName";
// Instance fields
private Northwind _db;
/// <summary>
/// Initializes a new instance of the <see cref="AlphabeticallistofproductsCollection_Base"/>
/// class with the specified <see cref="Northwind"/>.
/// </summary>
/// <param name="db">The <see cref="Northwind"/> object.</param>
public AlphabeticallistofproductsCollection_Base(Northwind db)
{
_db = db;
}
/// <summary>
/// Gets the database object that this view 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>Alphabetical list of products</c> view.
/// </summary>
/// <returns>An array of <see cref="AlphabeticallistofproductsRow"/> objects.</returns>
public virtual AlphabeticallistofproductsRow[] GetAll()
{
return MapRecords(CreateGetAllCommand());
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object that
/// includes all records from the <c>Alphabetical list of products</c> view.
/// </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>Alphabetical list of products</c> view.
/// </summary>
/// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
protected virtual IDbCommand CreateGetAllCommand()
{
return CreateGetCommand(null, null);
}
/// <summary>
/// Gets the first <see cref="AlphabeticallistofproductsRow"/> 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="AlphabeticallistofproductsRow"/> or null reference
/// (Nothing in Visual Basic) if the object was not found.</returns>
public AlphabeticallistofproductsRow GetRow(string whereSql)
{
int totalRecordCount = -1;
AlphabeticallistofproductsRow[] rows = GetAsArray(whereSql, null, 0, 1, ref totalRecordCount);
return 0 == rows.Length ? null : rows[0];
}
/// <summary>
/// Gets an array of <see cref="AlphabeticallistofproductsRow"/> 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="AlphabeticallistofproductsRow"/> objects.</returns>
public AlphabeticallistofproductsRow[] GetAsArray(string whereSql, string orderBySql)
{
int totalRecordCount = -1;
return GetAsArray(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
}
/// <summary>
/// Gets an array of <see cref="AlphabeticallistofproductsRow"/> 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="AlphabeticallistofproductsRow"/> objects.</returns>
public virtual AlphabeticallistofproductsRow[] 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].[Alphabetical list of products]";
if(null != whereSql && 0 < whereSql.Length)
sql += " WHERE " + whereSql;
if(null != orderBySql && 0 < orderBySql.Length)
sql += " ORDER BY " + orderBySql;
return _db.CreateCommand(sql);
}
/// <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="AlphabeticallistofproductsRow"/> objects.</returns>
protected AlphabeticallistofproductsRow[] 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 view.</param>
/// <returns>An array of <see cref="AlphabeticallistofproductsRow"/> objects.</returns>
protected AlphabeticallistofproductsRow[] 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 view.</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="AlphabeticallistofproductsRow"/> objects.</returns>
protected virtual AlphabeticallistofproductsRow[] 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 productIDColumnIndex = reader.GetOrdinal("ProductID");
int productNameColumnIndex = reader.GetOrdinal("ProductName");
int supplierIDColumnIndex = reader.GetOrdinal("SupplierID");
int categoryIDColumnIndex = reader.GetOrdinal("CategoryID");
int quantityPerUnitColumnIndex = reader.GetOrdinal("QuantityPerUnit");
int unitPriceColumnIndex = reader.GetOrdinal("UnitPrice");
int unitsInStockColumnIndex = reader.GetOrdinal("UnitsInStock");
int unitsOnOrderColumnIndex = reader.GetOrdinal("UnitsOnOrder");
int reorderLevelColumnIndex = reader.GetOrdinal("ReorderLevel");
int discontinuedColumnIndex = reader.GetOrdinal("Discontinued");
int categoryNameColumnIndex = reader.GetOrdinal("CategoryName");
System.Collections.ArrayList recordList = new System.Collections.ArrayList();
int ri = -startIndex;
while(reader.Read())
{
ri++;
if(ri > 0 && ri <= length)
{
AlphabeticallistofproductsRow record = new AlphabeticallistofproductsRow();
recordList.Add(record);
record.ProductID = Convert.ToInt32(reader.GetValue(productIDColumnIndex));
record.ProductName = Convert.ToString(reader.GetValue(productNameColumnIndex));
if(!reader.IsDBNull(supplierIDColumnIndex))
record.SupplierID = Convert.ToInt32(reader.GetValue(supplierIDColumnIndex));
if(!reader.IsDBNull(categoryIDColumnIndex))
record.CategoryID = Convert.ToInt32(reader.GetValue(categoryIDColumnIndex));
if(!reader.IsDBNull(quantityPerUnitColumnIndex))
record.QuantityPerUnit = Convert.ToString(reader.GetValue(quantityPerUnitColumnIndex));
if(!reader.IsDBNull(unitPriceColumnIndex))
record.UnitPrice = Convert.ToDecimal(reader.GetValue(unitPriceColumnIndex));
if(!reader.IsDBNull(unitsInStockColumnIndex))
record.UnitsInStock = Convert.ToInt16(reader.GetValue(unitsInStockColumnIndex));
if(!reader.IsDBNull(unitsOnOrderColumnIndex))
record.UnitsOnOrder = Convert.ToInt16(reader.GetValue(unitsOnOrderColumnIndex));
if(!reader.IsDBNull(reorderLevelColumnIndex))
record.ReorderLevel = Convert.ToInt16(reader.GetValue(reorderLevelColumnIndex));
record.Discontinued = Convert.ToBoolean(reader.GetValue(discontinuedColumnIndex));
record.CategoryName = Convert.ToString(reader.GetValue(categoryNameColumnIndex));
if(ri == length && 0 != totalRecordCount)
break;
}
}
totalRecordCount = 0 == totalRecordCount ? ri + startIndex : -1;
return (AlphabeticallistofproductsRow[])(recordList.ToArray(typeof(AlphabeticallistofproductsRow)));
}
/// <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 view.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
protected DataTable MapRecordsToDataTable(IDataReader reader)
{
int totalRecordCount = 0;
return MapRecordsToDataTable(reader, 0, int.MaxValue, ref totalRecordCount);
}
/// <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 view.</param>
/// <param name="startIndex">The index of the first record to read.</param>
/// <param name="length">The number of records to read.</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>
protected virtual DataTable MapRecordsToDataTable(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 columnCount = reader.FieldCount;
int ri = -startIndex;
DataTable dataTable = CreateDataTable();
dataTable.BeginLoadData();
object[] values = new object[columnCount];
while(reader.Read())
{
ri++;
if(ri > 0 && ri <= length)
{
reader.GetValues(values);
dataTable.LoadDataRow(values, true);
if(ri == length && 0 != totalRecordCount)
break;
}
}
dataTable.EndLoadData();
totalRecordCount = 0 == totalRecordCount ? ri + startIndex : -1;
return dataTable;
}
/// <summary>
/// Converts <see cref="System.Data.DataRow"/> to <see cref="AlphabeticallistofproductsRow"/>.
/// </summary>
/// <param name="row">The <see cref="System.Data.DataRow"/> object to be mapped.</param>
/// <returns>A reference to the <see cref="AlphabeticallistofproductsRow"/> object.</returns>
protected virtual AlphabeticallistofproductsRow MapRow(DataRow row)
{
AlphabeticallistofproductsRow mappedObject = new AlphabeticallistofproductsRow();
DataTable dataTable = row.Table;
DataColumn dataColumn;
// Column "ProductID"
dataColumn = dataTable.Columns["ProductID"];
if(!row.IsNull(dataColumn))
mappedObject.ProductID = (int)row[dataColumn];
// Column "ProductName"
dataColumn = dataTable.Columns["ProductName"];
if(!row.IsNull(dataColumn))
mappedObject.ProductName = (string)row[dataColumn];
// Column "SupplierID"
dataColumn = dataTable.Columns["SupplierID"];
if(!row.IsNull(dataColumn))
mappedObject.SupplierID = (int)row[dataColumn];
// Column "CategoryID"
dataColumn = dataTable.Columns["CategoryID"];
if(!row.IsNull(dataColumn))
mappedObject.CategoryID = (int)row[dataColumn];
// Column "QuantityPerUnit"
dataColumn = dataTable.Columns["QuantityPerUnit"];
if(!row.IsNull(dataColumn))
mappedObject.QuantityPerUnit = (string)row[dataColumn];
// Column "UnitPrice"
dataColumn = dataTable.Columns["UnitPrice"];
if(!row.IsNull(dataColumn))
mappedObject.UnitPrice = (decimal)row[dataColumn];
// Column "UnitsInStock"
dataColumn = dataTable.Columns["UnitsInStock"];
if(!row.IsNull(dataColumn))
mappedObject.UnitsInStock = (short)row[dataColumn];
// Column "UnitsOnOrder"
dataColumn = dataTable.Columns["UnitsOnOrder"];
if(!row.IsNull(dataColumn))
mappedObject.UnitsOnOrder = (short)row[dataColumn];
// Column "ReorderLevel"
dataColumn = dataTable.Columns["ReorderLevel"];
if(!row.IsNull(dataColumn))
mappedObject.ReorderLevel = (short)row[dataColumn];
// Column "Discontinued"
dataColumn = dataTable.Columns["Discontinued"];
if(!row.IsNull(dataColumn))
mappedObject.Discontinued = (bool)row[dataColumn];
// Column "CategoryName"
dataColumn = dataTable.Columns["CategoryName"];
if(!row.IsNull(dataColumn))
mappedObject.CategoryName = (string)row[dataColumn];
return mappedObject;
}
/// <summary>
/// Creates a <see cref="System.Data.DataTable"/> object for
/// the <c>Alphabetical list of products</c> view.
/// </summary>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
protected virtual DataTable CreateDataTable()
{
DataTable dataTable = new DataTable();
dataTable.TableName = "Alphabeticallistofproducts";
DataColumn dataColumn;
dataColumn = dataTable.Columns.Add("ProductID", typeof(int));
dataColumn.AllowDBNull = false;
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("ProductName", typeof(string));
dataColumn.MaxLength = 40;
dataColumn.AllowDBNull = false;
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("SupplierID", typeof(int));
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("CategoryID", typeof(int));
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("QuantityPerUnit", typeof(string));
dataColumn.MaxLength = 20;
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("UnitPrice", typeof(decimal));
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("UnitsInStock", typeof(short));
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("UnitsOnOrder", typeof(short));
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("ReorderLevel", typeof(short));
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("Discontinued", typeof(bool));
dataColumn.AllowDBNull = false;
dataColumn.ReadOnly = true;
dataColumn = dataTable.Columns.Add("CategoryName", typeof(string));
dataColumn.MaxLength = 15;
dataColumn.AllowDBNull = false;
dataColumn.ReadOnly = true;
return dataTable;
}
/// <summary>
/// Adds a new parameter to the specified command.
/// </summary>
/// <param name="cmd">The <see cref="System.Data.IDbCommand"/> object to add the parameter to.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <param name="value">The value of the parameter.</param>
/// <returns>A reference to the added parameter.</returns>
protected virtual IDbDataParameter AddParameter(IDbCommand cmd, string paramName, object value)
{
IDbDataParameter parameter;
switch(paramName)
{
case "ProductID":
parameter = _db.AddParameter(cmd, paramName, DbType.Int32, value);
break;
case "ProductName":
parameter = _db.AddParameter(cmd, paramName, DbType.String, value);
break;
case "SupplierID":
parameter = _db.AddParameter(cmd, paramName, DbType.Int32, value);
break;
case "CategoryID":
parameter = _db.AddParameter(cmd, paramName, DbType.Int32, value);
break;
case "QuantityPerUnit":
parameter = _db.AddParameter(cmd, paramName, DbType.String, value);
break;
case "UnitPrice":
parameter = _db.AddParameter(cmd, paramName, DbType.Currency, value);
break;
case "UnitsInStock":
parameter = _db.AddParameter(cmd, paramName, DbType.Int16, value);
break;
case "UnitsOnOrder":
parameter = _db.AddParameter(cmd, paramName, DbType.Int16, value);
break;
case "ReorderLevel":
parameter = _db.AddParameter(cmd, paramName, DbType.Int16, value);
break;
case "Discontinued":
parameter = _db.AddParameter(cmd, paramName, DbType.Boolean, value);
break;
case "CategoryName":
parameter = _db.AddParameter(cmd, paramName, DbType.String, value);
break;
default:
throw new ArgumentException("Unknown parameter name (" + paramName + ").");
}
return parameter;
}
} // End of AlphabeticallistofproductsCollection_Base class
} // End of namespace