EchoTex_Payroll/Ease.Core/DataAccess/Sql/SqlHelperExtension.cs

306 lines
12 KiB
C#
Raw Permalink Normal View History

2024-10-14 10:01:49 +06:00
/*
|-------------------------------------------------------------------------------|
| 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
/// <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)
{
DataAccessHelper.Fill(dataReader, dataSet, tableName, from, count);
}
/// <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)
{
SqlHelperExtension.Fill(dataReader, dataSet, tableName, 0, 0);
}
#endregion
#region Parameter
/// <summary>
/// Return SqlParameter object according to parameter.
/// </summary>
/// <param name="pType">Type of data.</param>
/// <param name="direction">Direction of parameter.</direction>
/// <param name="pValue">Value of parametetr.</param>
/// <returns>Instatnce of SqlParameter.</returns>
public static SqlParameter CreateParam(SqlDbType pType, ParameterDirection direction, object pValue)
{
return SqlHelperExtension.CreateParam(string.Empty, pType, direction, pValue);
}
/// <summary>
/// Return SqlParameter object according to parameter.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="direction">Direction of parameter.</param>
/// <returns>Instatnce of SqlParameter.</returns>
public static SqlParameter CreateParam(string pName, SqlDbType pType, ParameterDirection direction)
{
return SqlHelperExtension.CreateParam(pName, pType, direction, null);
}
/// <summary>
/// Return SqlParameter object according to parameter.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="direction">Direction of parameter.</direction>
/// <param name="pValue">Value of parametetr.</param>
/// <returns>Instatnce of SqlParameter.</returns>
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;
}
/// <summary>
/// Return SqlParameter object according to parameter.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="direction">Direction of parameter.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <param name="size">If varchar then lenth of value.</param>
/// <returns>Instatnce of SqlParameter.</returns>
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
/// <summary>
/// Return input direction SqlParameter object.
/// </summary>
/// <param name="pType">Type of data.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <returns>Instatnce of SqlParameter.</returns>
public static SqlParameter CreateInParam(SqlDbType pType, object pValue)
{
return SqlHelperExtension.CreateInParam(string.Empty, pType, pValue);
}
/// <summary>
/// Return input direction SqlParameter object.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <returns>Instatnce of SqlParameter.</returns>
public static SqlParameter CreateInParam(string pName, SqlDbType pType)
{
return SqlHelperExtension.CreateInParam(pName, pType, null);
}
/// <summary>
/// Return input direction SqlParameter object.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <returns>Instatnce of SqlParameter.</returns>
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;
}
/// <summary>
/// Return input direction SqlParameter object.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <param name="size">If varchar then lenth of value.</param>
/// <returns>Instatnce of SqlParameter.</returns>
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
/// <summary>
/// Return output direction SqlParameter object.
/// </summary>
/// <param name="pType">Type of data.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <returns>Instatnce of SqlParameter.</returns>
public static SqlParameter CreateOutParam(SqlDbType pType, object pValue)
{
return SqlHelperExtension.CreateOutParam(string.Empty, pType, pValue);
}
/// <summary>
/// Return output direction SqlParameter object.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <returns>Instatnce of SqlParameter.</returns>
public static SqlParameter CreateOutParam(string pName, SqlDbType pType)
{
return SqlHelperExtension.CreateOutParam(pName, pType, null);
}
/// <summary>
/// Return output direction SqlParameter object.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <returns>Instatnce of SqlParameter.</returns>
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;
}
/// <summary>
/// Return output direction SqlParameter object.
/// </summary>
/// <param name="pName">Name of parameter.</param>
/// <param name="pType">Type of data.</param>
/// <param name="pValue">Value of parametetr.</param>
/// <param name="size">If varchar then lenth of value.</param>
/// <returns>Instatnce of SqlParameter.</returns>
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
}