// <fileinfo name="OrdersCollection_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="OrdersCollection"/>. Provides methods
/// for common database table operations.
/// </summary>
/// <remarks>
/// Do not change this source code. Update the <see cref="OrdersCollection"/>
/// class if you need to add or change some functionality.
/// </remarks>
public abstract class OrdersCollection_Base
{
// Constants
public const string OrderIDColumnName = "OrderID";
public const string CustomerIDColumnName = "CustomerID";
public const string EmployeeIDColumnName = "EmployeeID";
public const string OrderDateColumnName = "OrderDate";
public const string RequiredDateColumnName = "RequiredDate";
public const string ShippedDateColumnName = "ShippedDate";
public const string ShipViaColumnName = "ShipVia";
public const string FreightColumnName = "Freight";
public const string ShipNameColumnName = "ShipName";
public const string ShipAddressColumnName = "ShipAddress";
public const string ShipCityColumnName = "ShipCity";
public const string ShipRegionColumnName = "ShipRegion";
public const string ShipPostalCodeColumnName = "ShipPostalCode";
public const string ShipCountryColumnName = "ShipCountry";
// Instance fields
private Northwind _db;
/// <summary>
/// Initializes a new instance of the <see cref="OrdersCollection_Base"/>
/// class with the specified <see cref="Northwind"/>.
/// </summary>
/// <param name="db">The <see cref="Northwind"/> object.</param>
public OrdersCollection_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>Orders</c> table.
/// </summary>
/// <returns>An array of <see cref="OrdersRow"/> objects.</returns>
public virtual OrdersRow[] GetAll()
{
return MapRecords(CreateGetAllCommand());
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object that
/// includes all records from the <c>Orders</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>Orders</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="OrdersRow"/> 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="OrdersRow"/> or null reference
/// (Nothing in Visual Basic) if the object was not found.</returns>
public OrdersRow GetRow(string whereSql)
{
int totalRecordCount = -1;
OrdersRow[] rows = GetAsArray(whereSql, null, 0, 1, ref totalRecordCount);
return 0 == rows.Length ? null : rows[0];
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> 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="OrdersRow"/> objects.</returns>
public OrdersRow[] GetAsArray(string whereSql, string orderBySql)
{
int totalRecordCount = -1;
return GetAsArray(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> 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="OrdersRow"/> objects.</returns>
public virtual OrdersRow[] 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].[Orders]";
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="OrdersRow"/> by the primary key.
/// </summary>
/// <param name="orderID">The <c>OrderID</c> column value.</param>
/// <returns>An instance of <see cref="OrdersRow"/> or null reference
/// (Nothing in Visual Basic) if the object was not found.</returns>
public virtual OrdersRow GetByPrimaryKey(int orderID)
{
string whereSql = "[OrderID]=" + _db.CreateSqlParameterName("OrderID");
IDbCommand cmd = CreateGetCommand(whereSql, null);
AddParameter(cmd, "OrderID", orderID);
OrdersRow[] tempArray = MapRecords(cmd);
return 0 == tempArray.Length ? null : tempArray[0];
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> objects
/// by the <c>FK_Orders_Customers</c> foreign key.
/// </summary>
/// <param name="customerID">The <c>CustomerID</c> column value.</param>
/// <returns>An array of <see cref="OrdersRow"/> objects.</returns>
public virtual OrdersRow[] GetByCustomerID(string customerID)
{
return MapRecords(CreateGetByCustomerIDCommand(customerID));
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Orders_Customers</c> foreign key.
/// </summary>
/// <param name="customerID">The <c>CustomerID</c> column value.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public virtual DataTable GetByCustomerIDAsDataTable(string customerID)
{
return MapRecordsToDataTable(CreateGetByCustomerIDCommand(customerID));
}
/// <summary>
/// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
/// return records by the <c>FK_Orders_Customers</c> foreign key.
/// </summary>
/// <param name="customerID">The <c>CustomerID</c> column value.</param>
/// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
protected virtual IDbCommand CreateGetByCustomerIDCommand(string customerID)
{
string whereSql = "";
if(null == customerID)
whereSql += "[CustomerID] IS NULL";
else
whereSql += "[CustomerID]=" + _db.CreateSqlParameterName("CustomerID");
IDbCommand cmd = CreateGetCommand(whereSql, null);
if(null != customerID)
AddParameter(cmd, "CustomerID", customerID);
return cmd;
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> objects
/// by the <c>FK_Orders_Employees</c> foreign key.
/// </summary>
/// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
/// <returns>An array of <see cref="OrdersRow"/> objects.</returns>
public OrdersRow[] GetByEmployeeID(int employeeID)
{
return GetByEmployeeID(employeeID, false);
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> objects
/// by the <c>FK_Orders_Employees</c> foreign key.
/// </summary>
/// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
/// <param name="employeeIDNull">true if the method ignores the employeeID
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>An array of <see cref="OrdersRow"/> objects.</returns>
public virtual OrdersRow[] GetByEmployeeID(int employeeID, bool employeeIDNull)
{
return MapRecords(CreateGetByEmployeeIDCommand(employeeID, employeeIDNull));
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Orders_Employees</c> foreign key.
/// </summary>
/// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public DataTable GetByEmployeeIDAsDataTable(int employeeID)
{
return GetByEmployeeIDAsDataTable(employeeID, false);
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Orders_Employees</c> foreign key.
/// </summary>
/// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
/// <param name="employeeIDNull">true if the method ignores the employeeID
/// 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 GetByEmployeeIDAsDataTable(int employeeID, bool employeeIDNull)
{
return MapRecordsToDataTable(CreateGetByEmployeeIDCommand(employeeID, employeeIDNull));
}
/// <summary>
/// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
/// return records by the <c>FK_Orders_Employees</c> foreign key.
/// </summary>
/// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
/// <param name="employeeIDNull">true if the method ignores the employeeID
/// 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 CreateGetByEmployeeIDCommand(int employeeID, bool employeeIDNull)
{
string whereSql = "";
if(employeeIDNull)
whereSql += "[EmployeeID] IS NULL";
else
whereSql += "[EmployeeID]=" + _db.CreateSqlParameterName("EmployeeID");
IDbCommand cmd = CreateGetCommand(whereSql, null);
if(!employeeIDNull)
AddParameter(cmd, "EmployeeID", employeeID);
return cmd;
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> objects
/// by the <c>FK_Orders_Shippers</c> foreign key.
/// </summary>
/// <param name="shipVia">The <c>ShipVia</c> column value.</param>
/// <returns>An array of <see cref="OrdersRow"/> objects.</returns>
public OrdersRow[] GetByShipVia(int shipVia)
{
return GetByShipVia(shipVia, false);
}
/// <summary>
/// Gets an array of <see cref="OrdersRow"/> objects
/// by the <c>FK_Orders_Shippers</c> foreign key.
/// </summary>
/// <param name="shipVia">The <c>ShipVia</c> column value.</param>
/// <param name="shipViaNull">true if the method ignores the shipVia
/// parameter value and uses DbNull instead of it; otherwise, false.</param>
/// <returns>An array of <see cref="OrdersRow"/> objects.</returns>
public virtual OrdersRow[] GetByShipVia(int shipVia, bool shipViaNull)
{
return MapRecords(CreateGetByShipViaCommand(shipVia, shipViaNull));
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Orders_Shippers</c> foreign key.
/// </summary>
/// <param name="shipVia">The <c>ShipVia</c> column value.</param>
/// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
public DataTable GetByShipViaAsDataTable(int shipVia)
{
return GetByShipViaAsDataTable(shipVia, false);
}
/// <summary>
/// Gets a <see cref="System.Data.DataTable"/> object
/// by the <c>FK_Orders_Shippers</c> foreign key.
/// </summary>
/// <param name="shipVia">The <c>ShipVia</c> column value.</param>
/// <param name="shipViaNull">true if the method ignores the shipVia
/// 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 GetByShipViaAsDataTable(int shipVia, bool shipViaNull)
{
return MapRecordsToDataTable(CreateGetByShipViaCommand(shipVia, shipViaNull));
}
/// <summary>
/// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
/// return records by the <c>FK_Orders_Shippers</c> foreign key.
/// </summary>
/// <param name="shipVia">The <c>ShipVia</c> column value.</param>
/// <param name="shipViaNull">true if the method ignores the shipVia
/// 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 CreateGetByShipViaCommand(int shipVia, bool shipViaNull)
{
string whereSql = "";
if(shipViaNull)
whereSql += "[ShipVia] IS NULL";
else
whereSql += "[ShipVia]=" + _db.CreateSqlParameterName("ShipVia");
IDbCommand cmd = CreateGetCommand(whereSql, null);
if(!shipViaNull)
AddParameter(cmd, "ShipVia", shipVia);
return cmd;
}
/// <summary>
/// Adds a new record into the <c>Orders</c> table.
/// </summary>
/// <param name="value">The <see cref="OrdersRow"/> object to be inserted.</param>
public virtual void Insert(OrdersRow value)
{
string sqlStr = "INSERT INTO [dbo].[Orders] (" +
"[CustomerID], " +
"[EmployeeID], " +
"[OrderDate], " +
"[RequiredDate], " +
"[ShippedDate], " +
"[ShipVia], " +
"[Freight], " +
"[ShipName], " +
"[ShipAddress], " +
"[ShipCity], " +
"[ShipRegion], " +
"[ShipPostalCode], " +
"[ShipCountry]" +
") VALUES (" +
_db.CreateSqlParameterName("CustomerID") + ", " +
_db.CreateSqlParameterName("EmployeeID") + ", " +
_db.CreateSqlParameterName("OrderDate") + ", " +
_db.CreateSqlParameterName("RequiredDate") + ", " +
_db.CreateSqlParameterName("ShippedDate") + ", " +
_db.CreateSqlParameterName("ShipVia") + ", " +
_db.CreateSqlParameterName("Freight") + ", " +
_db.CreateSqlParameterName("ShipName") + ", " +
_db.CreateSqlParameterName("ShipAddress") + ", " +
_db.CreateSqlParameterName("ShipCity") + ", " +
_db.CreateSqlParameterName("ShipRegion") + ", " +
_db.CreateSqlParameterName("ShipPostalCode") + ", " +
_db.CreateSqlParameterName("ShipCountry") + ");SELECT @@IDENTITY";
IDbCommand cmd = _db.CreateCommand(sqlStr);
AddParameter(cmd, "CustomerID", value.CustomerID);
AddParameter(cmd, "EmployeeID",
value.IsEmployeeIDNull ? DBNull.Value : (object)value.EmployeeID);
AddParameter(cmd, "OrderDate",
value.IsOrderDateNull ? DBNull.Value : (object)value.OrderDate);
AddParameter(cmd, "RequiredDate",
value.IsRequiredDateNull ? DBNull.Value : (object)value.RequiredDate);
AddParameter(cmd, "ShippedDate",
value.IsShippedDateNull ? DBNull.Value : (object)value.ShippedDate);
AddParameter(cmd, "ShipVia",
value.IsShipViaNull ? DBNull.Value : (object)value.ShipVia);
AddParameter(cmd, "Freight",
value.IsFreightNull ? DBNull.Value : (object)value.Freight);
AddParameter(cmd, "ShipName", value.ShipName);
AddParameter(cmd, "ShipAddress", value.ShipAddress);
AddParameter(cmd, "ShipCity", value.ShipCity);
AddParameter(cmd, "ShipRegion", value.ShipRegion);
AddParameter(cmd, "ShipPostalCode", value.ShipPostalCode);
AddParameter(cmd, "ShipCountry", value.ShipCountry);
value.OrderID = Convert.ToInt32(cmd.ExecuteScalar());
}
/// <summary>
/// Updates a record in the <c>Orders</c> table.
/// </summary>
/// <param name="value">The <see cref="OrdersRow"/>
/// object used to update the table record.</param>
/// <returns>true if the record was updated; otherwise, false.</returns>
public virtual bool Update(OrdersRow value)
{
string sqlStr = "UPDATE [dbo].[Orders] SET " +
"[CustomerID]=" + _db.CreateSqlParameterName("CustomerID") + ", " +
"[EmployeeID]=" + _db.CreateSqlParameterName("EmployeeID") + ", " +
"[OrderDate]=" + _db.CreateSqlParameterName("OrderDate") + ", " +
"[RequiredDate]=" + _db.CreateSqlParameterName("RequiredDate") + ", " +
"[ShippedDate]=" + _db.CreateSqlParameterName("ShippedDate") + ", " +
"[ShipVia]=" + _db.CreateSqlParameterName("ShipVia") + ", " +
"[Freight]=" + _db.CreateSqlParameterName("Freight") + ", " +
"[ShipName]=" + _db.CreateSqlParameterName("ShipName") + ", " +
"[ShipAddress]=" + _db.CreateSqlParameterName("ShipAddress") + ", " +
"[ShipCity]=" + _db.CreateSqlParameterName("ShipCity") + ", " +
"[ShipRegion]=" + _db.CreateSqlParameterName("ShipRegion") + ", " +
"[ShipPostalCode]=" + _db.CreateSqlParameterName("ShipPostalCode") + ", " +
"[ShipCountry]=" + _db.CreateSqlParameterName("ShipCountry") +
" WHERE " +
"[OrderID]=" + _db.CreateSqlParameterName("OrderID");
IDbCommand cmd = _db.CreateCommand(sqlStr);
AddParameter(cmd, "CustomerID", value.CustomerID);
AddParameter(cmd, "EmployeeID",
value.IsEmployeeIDNull ? DBNull.Value : (object)value.EmployeeID);
AddParameter(cmd, "OrderDate",
value.IsOrderDateNull ? DBNull.Value : (object)value.OrderDate);
AddParameter(cmd, "RequiredDate",
value.IsRequiredDateNull ? DBNull.Value : (object)value.RequiredDate);
AddParameter(cmd, "ShippedDate",
value.IsShippedDateNull ? DBNull.Value : (object)value.ShippedDate);
AddParameter(cmd, "ShipVia",
value.IsShipViaNull ? DBNull.Value : (object)value.ShipVia);
AddParameter(cmd, "Freight",
value.IsFreightNull ? DBNull.Value : (object)value.Freight);
AddParameter(cmd, "ShipName", value.ShipName);
AddParameter(cmd, "ShipAddress", value.ShipAddress);
AddParameter(cmd, "ShipCity", value.ShipCity);
AddParameter(cmd, "ShipRegion", value.ShipRegion);
AddParameter(cmd, "ShipPostalCode", value.ShipPostalCode);
AddParameter(cmd, "ShipCountry", value.ShipCountry);
AddParameter(cmd, "OrderID", value.OrderID);
return 0 != cmd.ExecuteNonQuery();
}
/// <summary>
/// Updates the <c>Orders</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>Orders</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"><