/* |-------------------------------------------------------------------------------| | Copyright © Computer Ease Limited | | Address: 1/9 Bloack-A Lalmatia, Dhaka-1207, Bangladesh | | Email: info@celimited.com, cease@bol-online.com, web: www.celimited.com | | Unauthorized copy or distribution is strictly prohibited | | Author: S. M. Russel, Last modified date: 23/07/2012 | |-------------------------------------------------------------------------------| */ using System; using System.Data; using Ease.Core.Utility; using Microsoft.Data.SqlClient; namespace Ease.Core.DataAccess.SQL { #region DataAccess: SQL Helper Extension public sealed class SqlHelperExtension { #region Constructor private SqlHelperExtension() { } #endregion #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) { DataAccessHelper.Fill(dataReader, dataSet, tableName, from, count); } /// /// 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) { SqlHelperExtension.Fill(dataReader, dataSet, tableName, 0, 0); } #endregion #region Parameter /// /// Return SqlParameter object according to parameter. /// /// Type of data. /// Direction of parameter. /// Value of parametetr. /// Instatnce of SqlParameter. public static SqlParameter CreateParam(SqlDbType pType, ParameterDirection direction, object pValue) { return SqlHelperExtension.CreateParam(string.Empty, pType, direction, pValue); } /// /// Return SqlParameter object according to parameter. /// /// Name of parameter. /// Type of data. /// Direction of parameter. /// Instatnce of SqlParameter. public static SqlParameter CreateParam(string pName, SqlDbType pType, ParameterDirection direction) { return SqlHelperExtension.CreateParam(pName, pType, direction, null); } /// /// Return SqlParameter object according to parameter. /// /// Name of parameter. /// Type of data. /// Direction of parameter. /// Value of parametetr. /// Instatnce of SqlParameter. public static SqlParameter CreateParam(string pName, SqlDbType pType, ParameterDirection direction, object pValue) { SqlParameter param = new SqlParameter(pName, pType); param.Direction = direction; if (pType == SqlDbType.VarChar) param.Size = 1000; param.Value = pValue; return param; } /// /// Return SqlParameter object according to parameter. /// /// Name of parameter. /// Type of data. /// Direction of parameter. /// Value of parametetr. /// If varchar then lenth of value. /// Instatnce of SqlParameter. public static SqlParameter CreateParam(string pName, SqlDbType pType, ParameterDirection direction, object pValue, int size) { SqlParameter param = new SqlParameter(pName, pType); param.Direction = direction; param.Size = size; if ((pType == SqlDbType.Char || pType == SqlDbType.VarChar || pType == SqlDbType.Text || pType == SqlDbType.NChar || pType == SqlDbType.NText || pType == SqlDbType.NVarChar) && size > 0) { string value = Global.StringFuncions.IsEmptyOrNull(pValue) ? string.Empty : pValue.ToString(); param.Value = Global.StringFuncions.Left(value, size); } else { param.Value = pValue; } return param; } #endregion #region Input Parameter /// /// Return input direction SqlParameter object. /// /// Type of data. /// Value of parametetr. /// Instatnce of SqlParameter. public static SqlParameter CreateInParam(SqlDbType pType, object pValue) { return SqlHelperExtension.CreateInParam(string.Empty, pType, pValue); } /// /// Return input direction SqlParameter object. /// /// Name of parameter. /// Type of data. /// Instatnce of SqlParameter. public static SqlParameter CreateInParam(string pName, SqlDbType pType) { return SqlHelperExtension.CreateInParam(pName, pType, null); } /// /// Return input direction SqlParameter object. /// /// Name of parameter. /// Type of data. /// Value of parametetr. /// Instatnce of SqlParameter. public static SqlParameter CreateInParam(string pName, SqlDbType pType, object pValue) { SqlParameter param = new SqlParameter(pName, pType); param.Direction = ParameterDirection.Input; if (pType == SqlDbType.VarChar) param.Size = 1000; param.Value = pValue; return param; } /// /// Return input direction SqlParameter object. /// /// Name of parameter. /// Type of data. /// Value of parametetr. /// If varchar then lenth of value. /// Instatnce of SqlParameter. public static SqlParameter CreateInParam(string pName, SqlDbType pType, object pValue, int size) { SqlParameter param = new SqlParameter(pName, pType); param.Direction = ParameterDirection.Input; param.Size = size; if ((pType == SqlDbType.Char || pType == SqlDbType.VarChar || pType == SqlDbType.Text || pType == SqlDbType.NChar || pType == SqlDbType.NText || pType == SqlDbType.NVarChar) && size > 0) { string value = Global.StringFuncions.IsEmptyOrNull(pValue) ? string.Empty : pValue.ToString(); param.Value = Global.StringFuncions.Left(value, size); } else { param.Value = pValue; } return param; } #endregion #region Output Parameter /// /// Return output direction SqlParameter object. /// /// Type of data. /// Value of parametetr. /// Instatnce of SqlParameter. public static SqlParameter CreateOutParam(SqlDbType pType, object pValue) { return SqlHelperExtension.CreateOutParam(string.Empty, pType, pValue); } /// /// Return output direction SqlParameter object. /// /// Name of parameter. /// Type of data. /// Instatnce of SqlParameter. public static SqlParameter CreateOutParam(string pName, SqlDbType pType) { return SqlHelperExtension.CreateOutParam(pName, pType, null); } /// /// Return output direction SqlParameter object. /// /// Name of parameter. /// Type of data. /// Value of parametetr. /// Instatnce of SqlParameter. public static SqlParameter CreateOutParam(string pName, SqlDbType pType, object pValue) { SqlParameter param = new SqlParameter(pName, pType); param.Direction = ParameterDirection.Output; if (pType == SqlDbType.VarChar) param.Size = 1000; param.Value = pValue; return param; } /// /// Return output direction SqlParameter object. /// /// Name of parameter. /// Type of data. /// Value of parametetr. /// If varchar then lenth of value. /// Instatnce of SqlParameter. public static SqlParameter CreateOutParam(string pName, SqlDbType pType, object pValue, int size) { SqlParameter param = new SqlParameter(pName, pType); param.Direction = ParameterDirection.Output; param.Size = size; param.Value = pValue; return param; } #endregion #region Add Parameter public static SqlCommand CreateCommand(string name) { SqlCommand command = new SqlCommand(name); return command; } public static void AddParameter(SqlCommand command, string name, SqlDbType type, ParameterDirection direction, object value) { SqlParameter param = command.CreateParameter(); param.ParameterName = name; param.SqlDbType = type; param.Direction = direction; if (type == SqlDbType.VarChar) param.Size = 1000; param.Value = value; command.Parameters.Add(param); } public static void AddParameter(SqlCommand command, string name, SqlDbType type, ParameterDirection direction) { SqlHelperExtension.AddParameter(command, name, type, direction, null); } #endregion } #endregion }