Willcode4cash

DBCommand Parameter Extensions

Sep 17th, 2016
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.01 KB | None | 0 0
  1. namespace MyApp.Extensions
  2. {
  3.     using System;
  4.     using System.Data;
  5.     using System.Data.Common;
  6.  
  7.     public static class DataObjectExtensions
  8.     {
  9.         /// <summary>A DbCommand extension method that adds a parameter.</summary>
  10.         /// <param name="command">The command to act on.</param>
  11.         /// <param name="name">The name.</param>
  12.         /// <param name="dbType">Type of the database.</param>
  13.         /// <returns>A DbParameter.</returns>
  14.         public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType)
  15.         {
  16.             DbParameter p = AddParameter(command, name, dbType, 0, ParameterDirection.Input);
  17.             return p;
  18.         }
  19.  
  20.         /// <summary>A DbCommand extension method that adds a parameter.</summary>
  21.         /// <param name="command">The command to act on.</param>
  22.         /// <param name="name">The name.</param>
  23.         /// <param name="dbType">Type of the database.</param>
  24.         /// <param name="value">The value.</param>
  25.         /// <returns>A DbParameter.</returns>
  26.         public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType, object value)
  27.         {
  28.             DbParameter p = AddParameter(command, name, dbType, 0, ParameterDirection.Input);
  29.             p.Value = value;
  30.             return p;
  31.         }
  32.  
  33.         /// <summary>A DbCommand extension method that adds a parameter.</summary>
  34.         /// <param name="command">The command to act on.</param>
  35.         /// <param name="name">The name.</param>
  36.         /// <param name="dbType">Type of the database.</param>
  37.         /// <param name="size">The size.</param>
  38.         /// <returns>A DbParameter.</returns>
  39.         public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType, int size)
  40.         {
  41.             return AddParameter(command, name, dbType, size, ParameterDirection.Input);
  42.         }
  43.  
  44.         /// <summary>A DbCommand extension method that adds a parameter.</summary>
  45.         /// <param name="command">The command to act on.</param>
  46.         /// <param name="name">The name.</param>
  47.         /// <param name="dbType">Type of the database.</param>
  48.         /// <param name="size">The size.</param>
  49.         /// <param name="direction">The direction.</param>
  50.         /// <returns>A DbParameter.</returns>
  51.         public static DbParameter AddParameter(this DbCommand command, string name, DbType dbType, int size, ParameterDirection direction)
  52.         {
  53.             DbParameter parameter = command.CreateParameter();
  54.             parameter.ParameterName = name;
  55.             parameter.DbType = dbType;
  56.             parameter.Direction = direction;
  57.             parameter.Size = size;
  58.             command.Parameters.Add(parameter);
  59.             return parameter;
  60.         }
  61.  
  62.         /// <summary>A DbConnection extension method that creates a command.</summary>
  63.         /// <param name="connection">The connection to act on.</param>
  64.         /// <param name="commandText">The command text.</param>
  65.         /// <returns>The new command.</returns>
  66.         public static DbCommand CreateCommand(this DbConnection connection, string commandText)
  67.         {
  68.             DbCommand command = connection.CreateCommand();
  69.             command.CommandText = commandText;
  70.             return command;
  71.         }
  72.  
  73.         /// <summary>A DbDataReader extension method that gets date time.</summary>
  74.         /// <param name="reader">The reader to act on.</param>
  75.         /// <param name="name">The name.</param>
  76.         /// <returns>The date time.</returns>
  77.         public static DateTime GetDBDateTime(this DbDataReader reader, string name)
  78.         {
  79.             int i = reader.GetOrdinal(name);
  80.             return reader.GetDateTime(i);
  81.         }
  82.  
  83.         /// <summary>A DbDataReader extension method that gets a decimal.</summary>
  84.         /// <param name="reader">The reader to act on.</param>
  85.         /// <param name="name">The name.</param>
  86.         /// <returns>The decimal.</returns>
  87.         public static decimal GetDBDecimal(this DbDataReader reader, string name)
  88.         {
  89.             int i = reader.GetOrdinal(name);
  90.             return reader.GetDecimal(i);
  91.         }
  92.  
  93.         /// <summary>A DbDataReader extension method that gets a double.</summary>
  94.         /// <param name="reader">The reader to act on.</param>
  95.         /// <param name="name">The name.</param>
  96.         /// <returns>The double.</returns>
  97.         public static double GetDBDouble(this DbDataReader reader, string name)
  98.         {
  99.             int i = reader.GetOrdinal(name);
  100.             return reader.GetDouble(i);
  101.         }
  102.  
  103.         /// <summary>A DbDataReader extension method that gets a string.</summary>
  104.         /// <param name="reader">The reader to act on.</param>
  105.         /// <param name="name">The name.</param>
  106.         /// <returns>The string.</returns>
  107.         public static string GetDBString(this DbDataReader reader, string name)
  108.         {
  109.             int i = reader.GetOrdinal(name);
  110.             return reader.GetString(i);
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment