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