using System; using System.Data; using Ease.Core.Utility; using System.Collections.Generic; namespace Ease.Core.DataAccess { #region Conntection context /// /// Summary description for ConnectionContext. /// Pass key of database connection according to config file /// 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 /// /// This class is used to help to fill dataset. /// internal class DataAccessHelper { #region Fill Data set /// /// Fills a typed DataSet using the DataReader's current result. This method /// allows paginated access to the database. /// /// The DataReader used to fetch the values. /// The DataSet used to store the values. /// The name of the DataSet table used to add the /// DataReader records. /// The quantity of records skipped before placing /// values on the DataReader on the DataSet. /// The maximum quantity of records alloed to fill on the /// DataSet. 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(); } /// /// Fills a typed DataSet using the DataReader's current result. This method /// allows paginated access to the database. /// /// The DataReader used to fetch the values. /// The DataSet used to store the values. /// The name of the DataSet table used to add the DataReader records. 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 } }