148 lines
4.6 KiB
C#
148 lines
4.6 KiB
C#
|
using System;
|
|||
|
using System.Data;
|
|||
|
using Ease.Core.Utility;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace Ease.Core.DataAccess
|
|||
|
{
|
|||
|
#region Conntection context
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Summary description for ConnectionContext.
|
|||
|
/// Pass key of database connection according to config file
|
|||
|
/// </summary>
|
|||
|
public sealed class ConnectionContext
|
|||
|
{
|
|||
|
private readonly string _ctxKey = string.Empty;
|
|||
|
|
|||
|
public ConnectionContext()
|
|||
|
{
|
|||
|
_ctxKey = "default";
|
|||
|
}
|
|||
|
|
|||
|
public ConnectionContext(string ctxKey)
|
|||
|
{
|
|||
|
_ctxKey = ctxKey;
|
|||
|
}
|
|||
|
|
|||
|
public string ContextKey
|
|||
|
{
|
|||
|
get { return _ctxKey; }
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// This class is used to help to fill dataset.
|
|||
|
/// </summary>
|
|||
|
internal class DataAccessHelper
|
|||
|
{
|
|||
|
#region Fill Data set
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Fills a typed DataSet using the DataReader's current result. This method
|
|||
|
/// allows paginated access to the database.
|
|||
|
/// </summary>
|
|||
|
/// <param name="dataReader">The DataReader used to fetch the values.</param>
|
|||
|
/// <param name="dataSet">The DataSet used to store the values.</param>
|
|||
|
/// <param name="tableName">The name of the DataSet table used to add the
|
|||
|
/// DataReader records.</param>
|
|||
|
/// <param name="from">The quantity of records skipped before placing
|
|||
|
/// values on the DataReader on the DataSet.</param>
|
|||
|
/// <param name="count">The maximum quantity of records alloed to fill on the
|
|||
|
/// DataSet.</param>
|
|||
|
public static void Fill(IDataReader dataReader, DataSet dataSet, string tableName, int from, int count)
|
|||
|
{
|
|||
|
string fieldName;
|
|||
|
DataTable fillTable;
|
|||
|
DataRow fillRow;
|
|||
|
int recNumber = 0;
|
|||
|
int totalRecords = from + count;
|
|||
|
|
|||
|
//If table name is null then set the string
|
|||
|
if (tableName == null)
|
|||
|
{
|
|||
|
tableName = "unknownTable";
|
|||
|
}
|
|||
|
|
|||
|
//If table does not exist in data set then add table
|
|||
|
if (dataSet.Tables[tableName] == null)
|
|||
|
{
|
|||
|
dataSet.Tables.Add(tableName);
|
|||
|
}
|
|||
|
|
|||
|
// Get the DataTable reference
|
|||
|
if (tableName == null)
|
|||
|
{
|
|||
|
fillTable = dataSet.Tables[0];
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
fillTable = dataSet.Tables[tableName];
|
|||
|
}
|
|||
|
|
|||
|
while (dataReader.Read())
|
|||
|
{
|
|||
|
if (recNumber++ >= from)
|
|||
|
{
|
|||
|
fillRow = fillTable.NewRow();
|
|||
|
for (int fieldIdx = 0; fieldIdx < dataReader.FieldCount; fieldIdx++)
|
|||
|
{
|
|||
|
fieldName = dataReader.GetName(fieldIdx);
|
|||
|
Type fieldType = dataReader.GetValue(fieldIdx).GetType();
|
|||
|
if (fillTable.Columns.IndexOf(fieldName) == -1)
|
|||
|
{
|
|||
|
fillTable.Columns.Add(fieldName, fieldType);
|
|||
|
}
|
|||
|
|
|||
|
fillRow[fieldName] = GetValue(dataReader.GetValue(fieldIdx), fieldType);
|
|||
|
}
|
|||
|
|
|||
|
fillTable.Rows.Add(fillRow);
|
|||
|
}
|
|||
|
|
|||
|
if (count != 0 && totalRecords <= recNumber)
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
dataSet.AcceptChanges();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Fills a typed DataSet using the DataReader's current result. This method
|
|||
|
/// allows paginated access to the database.
|
|||
|
/// </summary>
|
|||
|
/// <param name="dataReader">The DataReader used to fetch the values.</param>
|
|||
|
/// <param name="dataSet">The DataSet used to store the values.</param>
|
|||
|
/// <param name="tableName">The name of the DataSet table used to add the DataReader records.</param>
|
|||
|
public static void Fill(IDataReader dataReader, DataSet dataSet, string tableName)
|
|||
|
{
|
|||
|
Fill(dataReader, dataSet, tableName, 0, 0);
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region private function
|
|||
|
|
|||
|
private static object GetValue(object fieldValue, Type fieldType)
|
|||
|
{
|
|||
|
switch (fieldType.Name)
|
|||
|
{
|
|||
|
case "Int16":
|
|||
|
case "Int32":
|
|||
|
case "Int64":
|
|||
|
case "UInt16":
|
|||
|
case "UInt32":
|
|||
|
case "UInt64":
|
|||
|
return fieldValue == DBNull.Value ? 0 : fieldValue;
|
|||
|
}
|
|||
|
|
|||
|
return fieldValue;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|