namespace MyApp.Extensions
{
using System;
using System.Data;
using System.Data.Common;
public static class DataObjectExtensions
{
/// <summary>A DbCommand extension method that adds a parameter.</summary>
/// <param name="command">The command to act on.</param>
/// <param name="name">The name.</param>
/// <param name="dbType">Type of the database.</param>
/// <returns>A DbParameter.</returns>
public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType)
{
DbParameter p = AddParameter(command, name, dbType, 0, ParameterDirection.Input);
return p;
}
/// <summary>A DbCommand extension method that adds a parameter.</summary>
/// <param name="command">The command to act on.</param>
/// <param name="name">The name.</param>
/// <param name="dbType">Type of the database.</param>
/// <param name="value">The value.</param>
/// <returns>A DbParameter.</returns>
public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType, object value)
{
DbParameter p = AddParameter(command, name, dbType, 0, ParameterDirection.Input);
p.Value = value;
return p;
}
/// <summary>A DbCommand extension method that adds a parameter.</summary>
/// <param name="command">The command to act on.</param>
/// <param name="name">The name.</param>
/// <param name="dbType">Type of the database.</param>
/// <param name="size">The size.</param>
/// <returns>A DbParameter.</returns>
public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType, int size)
{
return AddParameter(command, name, dbType, size, ParameterDirection.Input);
}
/// <summary>A DbCommand extension method that adds a parameter.</summary>
/// <param name="command">The command to act on.</param>
/// <param name="name">The name.</param>
/// <param name="dbType">Type of the database.</param>
/// <param name="size">The size.</param>
/// <param name="direction">The direction.</param>
/// <returns>A DbParameter.</returns>
public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType, int size, ParameterDirection direction)
{
DbParameter parameter = command.CreateParameter();
parameter.ParameterName = name;
parameter.DbType = dbType;
parameter.Direction = direction;
parameter.Size = size;
command.Parameters.Add(parameter);
return parameter;
}
/// <summary>A DbConnection extension method that creates a command.</summary>
/// <param name="connection">The connection to act on.</param>
/// <param name="commandText">The command text.</param>
/// <returns>The new command.</returns>
public static DbCommand CreateCommand(this DbConnection connection, string commandText)
{
DbCommand command = connection.CreateCommand();
command.CommandText = commandText;
return command;
}
/// <summary>A DbDataReader extension method that gets date time.</summary>
/// <param name="reader">The reader to act on.</param>
/// <param name="name">The name.</param>
/// <returns>The date time.</returns>
public static DateTime GetDBDateTime(this DbDataReader reader, string name)
{
int i = reader.GetOrdinal(name);
return reader.GetDateTime(i);
}
/// <summary>A DbDataReader extension method that gets a decimal.</summary>
/// <param name="reader">The reader to act on.</param>
/// <param name="name">The name.</param>
/// <returns>The decimal.</returns>
public static decimal GetDBDecimal(this DbDataReader reader, string name)
{
int i = reader.GetOrdinal(name);
return reader.GetDecimal(i);
}
/// <summary>A DbDataReader extension method that gets a double.</summary>
/// <param name="reader">The reader to act on.</param>
/// <param name="name">The name.</param>
/// <returns>The double.</returns>
public static double GetDBDouble(this DbDataReader reader, string name)
{
int i = reader.GetOrdinal(name);
return reader.GetDouble(i);
}
/// <summary>A DbDataReader extension method that gets a string.</summary>
/// <param name="reader">The reader to act on.</param>
/// <param name="name">The name.</param>
/// <returns>The string.</returns>
public static string GetDBString(this DbDataReader reader, string name)
{
int i = reader.GetOrdinal(name);
return reader.GetString(i);
}
}
}
Comments