Cool ASP.NET 'trick' I've been working on..
Im taking a DataReader and slammin' it into a DataTable, but the idea is that I want to cache the DataTable in the Page Cache object, and create
DataViews of the data in the table for paging and sorting purposes.
This way I get a quick forward-only DataReader on the Page Load, with sorting and paging capabilities, in a biz-tier
data object, and the class is reusable for other biz-tier objects. (I have thousands of records in the database, so less time loading, and less round-tripping for paging/sorting, is essential.)
In order to do this, I wanted to have some recordID's for the paging, and in order to ensure we never clash with another PrimaryKey from the underlying
database (DataReader pull) I went with negative numbers. So I created an autoincrement column, filled the DataTable with the DataReader Schema first,
added the autoinc column, THEN fill the DataTable so I don't lose the autoincrement column.
The DataTable will now get returned to the caller, where I will slam it into the cache and create views, etc. off of it. Sweet :)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
namespace payc_bl
{
/// <summary>
/// Utilizes generic DBDataAdapter to return DataTables from DataReaders
/// </summary>
class PAYCDataTable : DbDataAdapter
{
public int FillFromReader(DataTable dataTable, IDataReader dataReader)
{
// Create a DataColumn and set various properties.
DataColumn myColumn = new DataColumn("recordID");
myColumn.DataType = System.Type.GetType("System.Int32");
myColumn.AutoIncrement = true;
myColumn.AutoIncrementSeed = -1;
myColumn.AutoIncrementStep = -1;
// Fill the DataTable with Schema, add the column, and set its Primary Key
dataTable = this.FillSchema(dataTable, SchemaType.Mapped, dataReader);
dataTable.Columns.Add(myColumn);
DataColumn[] keys = new DataColumn[1];
keys[0] = myColumn;
dataTable.PrimaryKey = keys;
// Fill the DataTable with the DataReader data
return this.Fill(dataTable, dataReader);
}
protected override RowUpdatedEventArgs CreateRowUpdatedEvent(
DataRow dataRow,
IDbCommand command,
StatementType statementType,
DataTableMapping tableMapping
) { return null; }
protected override RowUpdatingEventArgs CreateRowUpdatingEvent(
DataRow dataRow,
IDbCommand command,
StatementType statementType,
DataTableMapping tableMapping
) { return null; }
protected override void OnRowUpdated(
RowUpdatedEventArgs value
) { }
protected override void OnRowUpdating(
RowUpdatingEventArgs value
) { }
}
}