// <fileinfo name="ProductsCollection_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="ProductsCollection"/>. Provides methods
/// for common database table operations.
/// </summary>
/// <remarks>
/// Do not change this source code. Update the <see cref="ProductsCollection"/>
/// class if you need to add or change some functionality.
/// </remarks>
public abstract class ProductsCollection_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";
// Instance fields
private Northwind _db;
/// <summary>
/// Initializes a new instance of the <see cref="ProductsCollection_Base"/>
/// class with the specified <see cref="Northwind"/>.
/// </summary>
/// <param name="db">The <see cref="Northwind"/> object.</param>
public ProductsCollection_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>Products</c> table.
/// </summary>
/// <returns>An array of <see cref="ProductsRow"/> objects.</returns>
public virtual ProductsRow[] GetAll()
{
return MapRecords(CreateGetAllCommand());
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object that
/// includes all records from the <c>Products</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>Products</c> table.
/// </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="ProductsRow"/> 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="ProductsRow"/> or null reference
/// (Nothing in Visual Basic) if the object was not found.</returns>
public ProductsRow GetRow(string whereSql)
{
int totalRecordCount = -1;
ProductsRow[] rows = GetAsArray(whereSql, null, 0, 1, ref totalRecordCount);
return 0 == rows.Length ? null : rows[0];
}
/// <summary>
/// Gets an array of <see cref="ProductsRow"/> 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="ProductsRow"/> objects.</returns>
public ProductsRow[] GetAsArray(string whereSql, string orderBySql)
{
int totalRecordCount = -1;
return GetAsArray(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
}
/// <summary>
/// Gets an array of <see cref="ProductsRow"/> 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="ProductsRow"/> objects.</returns>
public virtual ProductsRow[] 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].[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>
/// Gets <see cref="ProductsRow"/> by the primary key.
/// </summary>
/// <param name="productID">The <c>ProductID</c> column value.</param>
/// <returns>An instance of <see cref="ProductsRow"/> or null reference
/// (Nothing in Visual Basic) if the object was not found.</returns>
public virtual ProductsRow GetByPrimaryKey(int productID)
{
string whereSql = "[ProductID]=" + _db.CreateSqlParameterName("ProductID");
IDbCommand cmd = CreateGetCommand(whereSql, null);
AddParameter(cmd, "ProductID", productID);
ProductsRow[] tempArray = MapRecords(cmd);
return 0 == tempArray.Length ? null : tempArray[0];
}
/// <summary>
/// Gets an array of <see cref="ProductsRow"/> objects
/// by the <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <returns>An array of <see cref="ProductsRow"/> objects.</returns>
public ProductsRow[] GetByCategoryID(int categoryID)
{
return GetByCategoryID(categoryID, false);
}
/// <summary>
/// Gets an array of <see cref="ProductsRow"/> objects
/// by the <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <param name="categoryIDNull">true if the method ignores the categoryID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>An array of <see cref="ProductsRow"/> objects.</returns>
public virtual ProductsRow[] GetByCategoryID(int categoryID, bool categoryIDNull)
{
return MapRecords(CreateGetByCategoryIDCommand(categoryID, categoryIDNull));
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public DataTable GetByCategoryIDAsDataTable(int categoryID)
{
return GetByCategoryIDAsDataTable(categoryID, false);
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <param name="categoryIDNull">true if the method ignores the categoryID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public virtual DataTable GetByCategoryIDAsDataTable(int categoryID, bool categoryIDNull)
{
return MapRecordsToDataTable(CreateGetByCategoryIDCommand(categoryID, categoryIDNull));
}
/// <summary>
/// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
/// return records by the <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <param name="categoryIDNull">true if the method ignores the categoryID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
protected virtual IDbCommand CreateGetByCategoryIDCommand(int categoryID, bool categoryIDNull)
{
string whereSql = "";
if(categoryIDNull)
whereSql += "[CategoryID] IS NULL";
else
whereSql += "[CategoryID]=" + _db.CreateSqlParameterName("CategoryID");
IDbCommand cmd = CreateGetCommand(whereSql, null);
if(!categoryIDNull)
AddParameter(cmd, "CategoryID", categoryID);
return cmd;
}
/// <summary>
/// Gets an array of <see cref="ProductsRow"/> objects
/// by the <c>FK_Products_Suppliers</c> foreign key.
/// </summary>
/// <param name="supplierID">The <c>SupplierID</c> column value.</param>
/// <returns>An array of <see cref="ProductsRow"/> objects.</returns>
public ProductsRow[] GetBySupplierID(int supplierID)
{
return GetBySupplierID(supplierID, false);
}
/// <summary>
/// Gets an array of <see cref="ProductsRow"/> objects
/// by the <c>FK_Products_Suppliers</c> foreign key.
/// </summary>
/// <param name="supplierID">The <c>SupplierID</c> column value.</param>
/// <param name="supplierIDNull">true if the method ignores the supplierID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>An array of <see cref="ProductsRow"/> objects.</returns>
public virtual ProductsRow[] GetBySupplierID(int supplierID, bool supplierIDNull)
{
return MapRecords(CreateGetBySupplierIDCommand(supplierID, supplierIDNull));
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Products_Suppliers</c> foreign key.
/// </summary>
/// <param name="supplierID">The <c>SupplierID</c> column value.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public DataTable GetBySupplierIDAsDataTable(int supplierID)
{
return GetBySupplierIDAsDataTable(supplierID, false);
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Products_Suppliers</c> foreign key.
/// </summary>
/// <param name="supplierID">The <c>SupplierID</c> column value.</param>
/// <param name="supplierIDNull">true if the method ignores the supplierID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public virtual DataTable GetBySupplierIDAsDataTable(int supplierID, bool supplierIDNull)
{
return MapRecordsToDataTable(CreateGetBySupplierIDCommand(supplierID, supplierIDNull));
}
/// <summary>
/// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
/// return records by the <c>FK_Products_Suppliers</c> foreign key.
/// </summary>
/// <param name="supplierID">The <c>SupplierID</c> column value.</param>
/// <param name="supplierIDNull">true if the method ignores the supplierID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
protected virtual IDbCommand CreateGetBySupplierIDCommand(int supplierID, bool supplierIDNull)
{
string whereSql = "";
if(supplierIDNull)
whereSql += "[SupplierID] IS NULL";
else
whereSql += "[SupplierID]=" + _db.CreateSqlParameterName("SupplierID");
IDbCommand cmd = CreateGetCommand(whereSql, null);
if(!supplierIDNull)
AddParameter(cmd, "SupplierID", supplierID);
return cmd;
}
/// <summary>
/// Adds a new record into the <c>Products</c> table.
/// </summary>
/// <param name="value">The <see cref="ProductsRow"/> object to be inserted.</param>
public virtual void Insert(ProductsRow value)
{
string sqlStr = "INSERT INTO [dbo].[Products] (" +
"[ProductName], " +
"[SupplierID], " +
"[CategoryID], " +
"[QuantityPerUnit], " +
"[UnitPrice], " +
"[UnitsInStock], " +
"[UnitsOnOrder], " +
"[ReorderLevel], " +
"[Discontinued]" +
") VALUES (" +
_db.CreateSqlParameterName("ProductName") + ", " +
_db.CreateSqlParameterName("SupplierID") + ", " +
_db.CreateSqlParameterName("CategoryID") + ", " +
_db.CreateSqlParameterName("QuantityPerUnit") + ", " +
_db.CreateSqlParameterName("UnitPrice") + ", " +
_db.CreateSqlParameterName("UnitsInStock") + ", " +
_db.CreateSqlParameterName("UnitsOnOrder") + ", " +
_db.CreateSqlParameterName("ReorderLevel") + ", " +
_db.CreateSqlParameterName("Discontinued") + ");SELECT @@IDENTITY";
IDbCommand cmd = _db.CreateCommand(sqlStr);
AddParameter(cmd, "ProductName", value.ProductName);
AddParameter(cmd, "SupplierID",
value.IsSupplierIDNull ? DBNull.Value : (object)value.SupplierID);
AddParameter(cmd, "CategoryID",
value.IsCategoryIDNull ? DBNull.Value : (object)value.CategoryID);
AddParameter(cmd, "QuantityPerUnit", value.QuantityPerUnit);
AddParameter(cmd, "UnitPrice",
value.IsUnitPriceNull ? DBNull.Value : (object)value.UnitPrice);
AddParameter(cmd, "UnitsInStock",
value.IsUnitsInStockNull ? DBNull.Value : (object)value.UnitsInStock);
AddParameter(cmd, "UnitsOnOrder",
value.IsUnitsOnOrderNull ? DBNull.Value : (object)value.UnitsOnOrder);
AddParameter(cmd, "ReorderLevel",
value.IsReorderLevelNull ? DBNull.Value : (object)value.ReorderLevel);
AddParameter(cmd, "Discontinued", value.Discontinued);
value.ProductID = Convert.ToInt32(cmd.ExecuteScalar());
}
/// <summary>
/// Updates a record in the <c>Products</c> table.
/// </summary>
/// <param name="value">The <see cref="ProductsRow"/>
/// object used to update the table record.</param>
/// <returns>true if the record was updated; otherwise, false.</returns>
public virtual bool Update(ProductsRow value)
{
string sqlStr = "UPDATE [dbo].[Products] SET " +
"[ProductName]=" + _db.CreateSqlParameterName("ProductName") + ", " +
"[SupplierID]=" + _db.CreateSqlParameterName("SupplierID") + ", " +
"[CategoryID]=" + _db.CreateSqlParameterName("CategoryID") + ", " +
"[QuantityPerUnit]=" + _db.CreateSqlParameterName("QuantityPerUnit") + ", " +
"[UnitPrice]=" + _db.CreateSqlParameterName("UnitPrice") + ", " +
"[UnitsInStock]=" + _db.CreateSqlParameterName("UnitsInStock") + ", " +
"[UnitsOnOrder]=" + _db.CreateSqlParameterName("UnitsOnOrder") + ", " +
"[ReorderLevel]=" + _db.CreateSqlParameterName("ReorderLevel") + ", " +
"[Discontinued]=" + _db.CreateSqlParameterName("Discontinued") +
" WHERE " +
"[ProductID]=" + _db.CreateSqlParameterName("ProductID");
IDbCommand cmd = _db.CreateCommand(sqlStr);
AddParameter(cmd, "ProductName", value.ProductName);
AddParameter(cmd, "SupplierID",
value.IsSupplierIDNull ? DBNull.Value : (object)value.SupplierID);
AddParameter(cmd, "CategoryID",
value.IsCategoryIDNull ? DBNull.Value : (object)value.CategoryID);
AddParameter(cmd, "QuantityPerUnit", value.QuantityPerUnit);
AddParameter(cmd, "UnitPrice",
value.IsUnitPriceNull ? DBNull.Value : (object)value.UnitPrice);
AddParameter(cmd, "UnitsInStock",
value.IsUnitsInStockNull ? DBNull.Value : (object)value.UnitsInStock);
AddParameter(cmd, "UnitsOnOrder",
value.IsUnitsOnOrderNull ? DBNull.Value : (object)value.UnitsOnOrder);
AddParameter(cmd, "ReorderLevel",
value.IsReorderLevelNull ? DBNull.Value : (object)value.ReorderLevel);
AddParameter(cmd, "Discontinued", value.Discontinued);
AddParameter(cmd, "ProductID", value.ProductID);
return 0 != cmd.ExecuteNonQuery();
}
/// <summary>
/// Updates the <c>Products</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>Products</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["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>Products</c> table.
/// </summary>
/// <param name="value">The <see cref="ProductsRow"/> object to delete.</param>
/// <returns>true if the record was deleted; otherwise, false.</returns>
public bool Delete(ProductsRow value)
{
return DeleteByPrimaryKey(value.ProductID);
}
/// <summary>
/// Deletes a record from the <c>Products</c> table using
/// the specified primary key.
/// </summary>
/// <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 productID)
{
string whereSql = "[ProductID]=" + _db.CreateSqlParameterName("ProductID");
IDbCommand cmd = CreateDeleteCommand(whereSql);
AddParameter(cmd, "ProductID", productID);
return 0 < cmd.ExecuteNonQuery();
}
/// <summary>
/// Deletes records from the <c>Products</c> table using the
/// <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <returns>The number of records deleted from the table.</returns>
public int DeleteByCategoryID(int categoryID)
{
return DeleteByCategoryID(categoryID, false);
}
/// <summary>
/// Deletes records from the <c>Products</c> table using the
/// <c>FK_Products_Categories</c> foreign key.
/// </summary>
/// <param name="categoryID">The <c>CategoryID</c> column value.</param>
/// <param name="categoryIDNull">true if the method ignores the categoryID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>The number of records deleted from the table.</returns>
public int DeleteByCategoryID(int categoryID, bool categoryIDNull)
{
return CreateDeleteByCategoryIDCommand(categoryID, categoryIDNull).ExecuteNonQuery();
}
/// <summary>
/// Creates an <see cref="System.Data.IDbCommand"/>