title: DBManager Retieve parameter by name instead of index
body: <p>I found a DBManager class a long time ago and have been using it successfully for well over a year. There is one 'feature' it is lacking and that is retrieving parameters by name rather than index. At present I use:</p>
  
  <pre><code>  using (IDBManager dbManager = new DBManager())
    {
      try
      {
        dbManager.Open();
        dbManager.CreateParameters(8);
        dbManager.AddParameters(0, "@Court", 0);
        dbManager.AddParameters(1, "@BookingDate", DateTime.Today);
        dbManager.AddParameters(2, "@BookingNumber", bookingNumber + 0.01M);
        dbManager.AddParameters(3, "@EventID", eventID);
        dbManager.AddParameters(4, "@BookingStartDT", BookingStartDT);
        dbManager.AddParameters(5, "@BookingFinishDT", BookingFinishDT);
        dbManager.AddParameters(6, "@CourseID", courseID);
        dbManager.AddParameters(7, "@ProSessionID", proSessionID);
  </code></pre>
  
  <p>etc to create the instance and parameters but need to always use the format:</p>
  
  <pre><code>            dbManager.Parameters[1].Value = requestedDate;
  </code></pre>
  
  <p>in my code.</p>
  
  <p>I would like to be able to use the parameter's name as in</p>
  
  <pre><code>            dbManager.Parameters["@BookingDate"].Value = requestedDate;
  </code></pre>
  
  <p>as I'm sick of scrolling thru my code to check what the index number is.</p>
  
  <p>I basically understand using an Interface but am certainly a hacker when it comes to writing one. </p>
  
  <p>In my interface IDBManager Class I have:</p>
  
  <pre><code>IDbDataParameter[] Parameters
  {
    get;
  }
  </code></pre>
  
  <p>In my sealed DBManagerFactory Class I have:</p>
  
  <pre><code>public static IDbDataParameter[] GetParameters(DataProvider providerType, int paramsCount)
  {
    IDbDataParameter[] idbParams = new IDbDataParameter[paramsCount];
  
    switch (providerType)
    {
      case DataProvider.SqlServer:
        for (int i = 0; i &lt; paramsCount; ++i)
        {
          idbParams[i] = new SqlParameter();
        }
        break;
  
      case DataProvider.SqlExpress:
        for (int i = 0; i &lt; paramsCount; ++i)
        {
          idbParams[i] = new SqlParameter();
        }
        break;
  
      case DataProvider.VistaDB:
        for (int i = 0; i &lt; paramsCount; ++i)
        {
          idbParams[i] = new VistaDBParameter();
        }
        break;
  
      case DataProvider.SqlCE:
        for (int i = 0; i &lt; paramsCount; ++i)
        {
          idbParams[i] = new SqlCeParameter();
        }
        break;
      case DataProvider.Oracle:
        for (int i = 0; i &lt; paramsCount; ++i)
        {
          idbParams[i] = new OracleParameter();
        }
        break;
  
      case DataProvider.Odbc:
        for (int i = 0; i &lt; paramsCount; ++i)
        {
          idbParams[i] = new OdbcParameter();
        }
        break;
  
      default:
        idbParams = null;
        break;
    }
    return idbParams;
   }
  }
  </code></pre>
  
  <p>In my sealed class DBManager I have:</p>
  
  <pre><code>public IDbDataParameter[] Parameters
  {
    get
    {
      return idbParameters;
    }
  }
  </code></pre>
  
  <p>What code do I need to add to my classes to achieve the desired result? I would appreciate some simple explanation as well as I do want to use this as a good training example in writing interfaces and how they work.</p>
  
  <p>Thanks in advance...</p>
  
  <p>EDIT: The following is a full copy of the three classes involved..</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The DataProvider's available.
  /// &lt;/summary&gt;
  public enum DataProvider
  {
      /// &lt;summary&gt;
      /// VistaDB
      /// &lt;/summary&gt;
      VistaDB,
      /// &lt;summary&gt;
      /// SqlServer
      /// &lt;/summary&gt;
      SqlServer,
  
      /// &lt;summary&gt;
      /// SQL Express
      /// &lt;/summary&gt;
      SqlExpress,
  
      /// &lt;summary&gt;
      /// SQL CE
      /// &lt;/summary&gt;
      SqlCE,
  
      /// &lt;summary&gt;
      /// Oracle
      /// &lt;/summary&gt;
      Oracle,
  
      /// &lt;summary&gt;
      /// ODBC
      /// &lt;/summary&gt;
      Odbc
  }
  
  /// &lt;summary&gt;
  /// IDBManager Interface: Provides access to the DBManagerFactory sealed class.
  /// &lt;/summary&gt;
  public interface IDBManager : IDisposable
  {
  /// &lt;summary&gt;
  /// Gets or sets the type of the provider.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The type of the provider.&lt;/value&gt;
  DataProvider ProviderType
  {
      get;
      set;
  }
  
  /// &lt;summary&gt;
  /// Gets or sets the connection string.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The connection string.&lt;/value&gt;
  string ConnectionString
  {
      get;
      set;
  }
  
  /// &lt;summary&gt;
  /// Gets the connection.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The connection.&lt;/value&gt;
  IDbConnection Connection
  {
      get;
  }
  /// &lt;summary&gt;
  /// Gets the transaction.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The transaction.&lt;/value&gt;
  IDbTransaction Transaction
  {
      get;
  }
  
  /// &lt;summary&gt;
  /// Gets the data reader.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The data reader.&lt;/value&gt;
  IDataReader DataReader
  {
      get;
  }
  /// &lt;summary&gt;
  /// Gets the command.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The command.&lt;/value&gt;
  IDbCommand Command
  {
      get;
  }
  
  /// &lt;summary&gt;
  /// Gets the parameters.
  /// &lt;/summary&gt;
  /// &lt;value&gt;The parameters.&lt;/value&gt;
  IDbDataParameter[] Parameters
  {
      get;
  }
  
  
  /// &lt;summary&gt;
  /// Opens this instance.
  /// &lt;/summary&gt;
  void Open();
  /// &lt;summary&gt;
  /// Begins the transaction.
  /// &lt;/summary&gt;
  void BeginTransaction();
  /// &lt;summary&gt;
  /// Commits the transaction.
  /// &lt;/summary&gt;
  void CommitTransaction();
  
  
  /// &lt;summary&gt;
  /// Rolls the back transaction.
  /// &lt;/summary&gt;
  void RollBackTransaction();
  
  /// &lt;summary&gt;
  /// Creates the parameters.
  /// &lt;/summary&gt;
  /// &lt;param name="paramsCount"&gt;The params count.&lt;/param&gt;
  void CreateParameters(int paramsCount);
  /// &lt;summary&gt;
  /// Adds the parameters.
  /// &lt;/summary&gt;
  /// &lt;param name="index"&gt;The index.&lt;/param&gt;
  /// &lt;param name="paramName"&gt;Name of the param.&lt;/param&gt;
  /// &lt;param name="objValue"&gt;The obj value.&lt;/param&gt;
  void AddParameters(int index, string paramName, object objValue);
  
  /// &lt;summary&gt;
  /// The command to be utilised by the reader.
  /// &lt;/summary&gt;
  /// &lt;param name="commandType"&gt;Type of the command.&lt;/param&gt;
  /// &lt;param name="commandText"&gt;The command text.&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  IDataReader ExecuteReader(CommandType commandType, string commandText);
  
  /// &lt;summary&gt;
  /// The command to be utilised to fill the dataset.
  /// &lt;/summary&gt;
  /// &lt;param name="commandType"&gt;Type of the command.&lt;/param&gt;
  /// &lt;param name="commandText"&gt;The command text.&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  DataSet ExecuteDataSet(CommandType commandType, string commandText);
  
  
  /// &lt;summary&gt;
  /// Creates a new datatable containg the columns specified in the command. Replaces any current table data.
  /// &lt;/summary&gt;
  /// &lt;param name="commandType"&gt;Type of the command.&lt;/param&gt;
  /// &lt;param name="commandText"&gt;The command text.&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  DataTable ExecuteDataTable(CommandType commandType, string commandText);
  
  
  /// &lt;summary&gt;
  /// Executes the command and returns the result as an object.
  /// &lt;/summary&gt;
  /// &lt;param name="CommandType"&gt;Type of the command.&lt;/param&gt;
  /// &lt;param name="CommandText"&gt;The command text.&lt;/param&gt;
  /// &lt;returns&gt;The result of the command as an object.&lt;/returns&gt;
  object ExecuteScalar(CommandType CommandType, string CommandText);
  
  /// &lt;summary&gt;
  /// Executes the command and returns the number of rows affected as an int.
  /// &lt;/summary&gt;
  /// &lt;param name="CommandType"&gt;Type of the command.&lt;/param&gt;
  /// &lt;param name="CommandText"&gt;The command text.&lt;/param&gt;
  /// &lt;returns&gt;Returns the number of rows affected.&lt;/returns&gt;
  int ExecuteNonQuery(CommandType CommandType, string CommandText);
  /// &lt;summary&gt;
  /// Closes the reader.
  /// &lt;/summary&gt;
  void CloseReader();
  /// &lt;summary&gt;
  /// Closes this instance.
  /// &lt;/summary&gt;
  void Close();
  /// &lt;summary&gt;
  /// Releases unmanaged and - optionally - managed resources
  /// &lt;/summary&gt;
  //void Dispose();
  
  }
  
  /// &lt;summary&gt;
  /// DBManagerFactory: Sealed class and cannot be inherited.
  /// &lt;/summary&gt;
  public sealed class DBManagerFactory
  {
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="DBManagerFactory"/&gt; class.
      /// &lt;/summary&gt;
      private DBManagerFactory() { }
  
  
      /// &lt;summary&gt;
      /// Gets the connection.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IDbConnection GetConnection(DataProvider providerType)
      {
          IDbConnection iDbConnection = null;
          switch (providerType)
          {
              case DataProvider.SqlServer:
                  iDbConnection = new SqlConnection();
                  break;
              case DataProvider.SqlExpress:
                  iDbConnection = new SqlConnection();
                  break;
              case DataProvider.VistaDB:
                  iDbConnection = new VistaDBConnection();
                  break;
              case DataProvider.SqlCE:
                  iDbConnection = new SqlCeConnection();
                  break;
              case DataProvider.Oracle:
                  iDbConnection = new OracleConnection();
                  break;
              case DataProvider.Odbc:
                  iDbConnection = new OdbcConnection();
                  break;
              default:
                  return null;
          }
          return iDbConnection;
      }
  
      /// &lt;summary&gt;
      /// Gets the command.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IDbCommand GetCommand(DataProvider providerType)
      {
          switch (providerType)
          {
              case DataProvider.SqlServer:
                  return new SqlCommand();
              case DataProvider.SqlExpress:
                  return new SqlCommand();
              case DataProvider.VistaDB:
                  return new VistaDBCommand();
              case DataProvider.SqlCE:
                  return new SqlCeCommand();
              case DataProvider.Oracle:
                  return new OracleCommand();
              case DataProvider.Odbc:
                  return new OdbcCommand();
              default:
                  return null;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets the data adapter.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IDbDataAdapter GetDataAdapter(DataProvider providerType)
      {
          switch (providerType)
          {
              case DataProvider.SqlServer:
                  return new SqlDataAdapter();
              case DataProvider.SqlExpress:
                  return new SqlDataAdapter();
              case DataProvider.VistaDB:
                  return new VistaDBDataAdapter();
              case DataProvider.SqlCE:
                  return new SqlCeDataAdapter();
              case DataProvider.Oracle:
                  return new OracleDataAdapter();
              case DataProvider.Odbc:
                  return new OdbcDataAdapter();
              default:
                  return null;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets the transaction.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IDbTransaction GetTransaction(DataProvider providerType)
      {
          IDbConnection iDbConnection = GetConnection(providerType);
          IDbTransaction iDbTransaction = iDbConnection.BeginTransaction();
          return iDbTransaction;
      }
  
      /// &lt;summary&gt;
      /// Gets the parameter.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IDataParameter GetParameter(DataProvider providerType)
      {
          IDataParameter iDataParameter = null;
          switch (providerType)
          {
              case DataProvider.SqlServer:
                  iDataParameter = new SqlParameter();
                  break;
              case DataProvider.SqlExpress:
                  iDataParameter = new SqlParameter();
                  break;
              case DataProvider.VistaDB:
                  iDataParameter = new VistaDBParameter();
                  break;
              case DataProvider.SqlCE:
                  iDataParameter = new SqlCeParameter();
                  break;
              case DataProvider.Oracle:
                  iDataParameter = new OracleParameter();
                  break;
              case DataProvider.Odbc:
                  iDataParameter = new OdbcParameter();
                  break;
  
          }
          return iDataParameter;
      }
  
      /// &lt;summary&gt;
      /// Gets the parameters.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;param name="paramsCount"&gt;The params count.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static IDbDataParameter[] GetParameters(DataProvider providerType, int paramsCount)
      {
          IDbDataParameter[] idbParams = new IDbDataParameter[paramsCount];
  
          switch (providerType)
          {
              case DataProvider.SqlServer:
                  for (int i = 0; i &lt; paramsCount; ++i)
                  {
                      idbParams[i] = new SqlParameter();
                  }
                  break;
  
              case DataProvider.SqlExpress:
                  for (int i = 0; i &lt; paramsCount; ++i)
                  {
                      idbParams[i] = new SqlParameter();
                  }
                  break;
  
              case DataProvider.VistaDB:
                  for (int i = 0; i &lt; paramsCount; ++i)
                  {
                      idbParams[i] = new VistaDBParameter();
                  }
                  break;
  
              case DataProvider.SqlCE:
                  for (int i = 0; i &lt; paramsCount; ++i)
                  {
                      idbParams[i] = new SqlCeParameter();
                  }
                  break;
              case DataProvider.Oracle:
                  for (int i = 0; i &lt; paramsCount; ++i)
                  {
                      idbParams[i] = new OracleParameter();
                  }
                  break;
  
              case DataProvider.Odbc:
                  for (int i = 0; i &lt; paramsCount; ++i)
                  {
                      idbParams[i] = new OdbcParameter();
                  }
                  break;
  
              default:
                  idbParams = null;
                  break;
          }
          return idbParams;
      }
  }
  
  
  /// &lt;summary&gt;
  /// DBManager 
  /// &lt;/summary&gt;
  public sealed class DBManager : IDBManager, IDisposable
  {
      private IDbConnection idbConnection;
      private IDataReader idataReader;
      private IDbCommand idbCommand;
      private DataProvider providerType;
      private IDbTransaction idbTransaction = null;
      private IDbDataParameter[] idbParameters = null;
      private string strConnection;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="DBManager"/&gt; class.
      /// &lt;/summary&gt;
      public DBManager()
      {
          //DataProvider defaultDataProvider = DataProvider.VistaDB;
          //if (Config.DALProvider != null)
          //  defaultDataProvider = Config.DALProvider;
  
          //switch (defaultDataProvider)
          //{
          //  case(DataProvider.VistaDB):
          //    providerType = DataProvider.VistaDB;
          //    break;
  
  
  
  
  
          //}
          //Config.DALProvider = DataProvider.VistaDB; // &lt;--------------------- Needs adding to Config.xyz
          this.providerType = Config.DALProvider;
          this.ConnectionString = CommonData.vdbConnectionString; //.sqlExpressConnectionString;
          //.vdbConnectionString; //.sqlExpressConnectionString; // CommonData.vdbConnectionString;
      }
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="DBManager"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      public DBManager(DataProvider providerType)
      {
          this.providerType = providerType;
      }
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="DBManager"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="providerType"&gt;Type of the provider.&lt;/param&gt;
      /// &lt;param name="connectionString"&gt;The connection string.&lt;/param&gt;
      public DBManager(DataProvider providerType, string connectionString)
      {
          this.providerType = providerType;
          this.strConnection = connectionString;
      }
  
      /// &lt;summary&gt;
      /// Gets the connection.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The connection.&lt;/value&gt;
      public IDbConnection Connection
      {
          get
          {
              return idbConnection;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets the data reader.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The data reader.&lt;/value&gt;
      public IDataReader DataReader
      {
          get
          {
              return idataReader;
          }
          set
          {
              idataReader = value;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the type of the provider.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The type of the provider.&lt;/value&gt;
      public DataProvider ProviderType
      {
          get
          {
              return providerType;
          }
          set
          {
              providerType = value;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the connection string.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The connection string.&lt;/value&gt;
      public string ConnectionString
      {
          get
          {
              return strConnection;
          }
          set
          {
              strConnection = value;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets the command.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The command.&lt;/value&gt;
      public IDbCommand Command
      {
          get
          {
              return idbCommand;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets the transaction.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The transaction.&lt;/value&gt;
      public IDbTransaction Transaction
      {
          get
          {
              return idbTransaction;
          }
      }
  
      /// &lt;summary&gt;
      /// Gets the parameters.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The parameters.&lt;/value&gt;
      public IDbDataParameter[] Parameters
      {
          get
          {
              return idbParameters;
          }
      }
  
      /// &lt;summary&gt;
      /// Opens this instance.
      /// &lt;/summary&gt;
      public void Open()
      {
          idbConnection = DBManagerFactory.GetConnection(this.providerType);
          idbConnection.ConnectionString = this.ConnectionString;
          if (idbConnection.State != ConnectionState.Open)
              idbConnection.Open();
          this.idbCommand = DBManagerFactory.GetCommand(this.ProviderType);
      }
  
      /// &lt;summary&gt;
      /// Closes this instance.
      /// &lt;/summary&gt;
      public void Close()
      {
          if (idbConnection != null &amp;&amp; idbConnection.State != ConnectionState.Closed)
              idbConnection.Close();
      }
  
      /// &lt;summary&gt;
      /// Releases unmanaged and - optionally - managed resources
      /// &lt;/summary&gt;
      public void Dispose()
      {
          GC.SuppressFinalize(this);
          this.Close();
          this.idbCommand = null;
          this.idbTransaction = null;
          this.idbConnection = null;
  
          if (ProviderType == DataProvider.VistaDB)
              VistaDBConnection.ClearAllPools();
      }
  
      /// &lt;summary&gt;
      /// Creates the parameters.
      /// &lt;/summary&gt;
      /// &lt;param name="paramsCount"&gt;The params count.&lt;/param&gt;
      public void CreateParameters(int paramsCount)
      {
          idbParameters = new IDbDataParameter[paramsCount];
          idbParameters = DBManagerFactory.GetParameters(this.ProviderType, paramsCount);
      }
  
      /// &lt;summary&gt;
      /// Adds the parameters.
      /// &lt;/summary&gt;
      /// &lt;param name="index"&gt;The index.&lt;/param&gt;
      /// &lt;param name="paramName"&gt;Name of the param.&lt;/param&gt;
      /// &lt;param name="objValue"&gt;The obj value.&lt;/param&gt;
      public void AddParameters(int index, string paramName, object objValue)
      {
          if (index &lt; idbParameters.Length)
          {
              idbParameters[index].ParameterName = paramName;
              idbParameters[index].Value = objValue;
          }
      }
  
      /// &lt;summary&gt;
      /// Begins the transaction.
      /// &lt;/summary&gt;
      public void BeginTransaction()
      {
          if (this.idbTransaction == null)
              idbTransaction = DBManagerFactory.GetTransaction(this.ProviderType);
          this.idbCommand.Transaction = idbTransaction;
      }
  
      /// &lt;summary&gt;
      /// Commits the transaction.
      /// &lt;/summary&gt;
      public void CommitTransaction()
      {
          if (this.idbTransaction != null)
              this.idbTransaction.Commit();
          idbTransaction = null;
      }
  
      /// &lt;summary&gt;
      /// Rolls back the transaction.
      /// &lt;/summary&gt;
      public void RollBackTransaction()
      {
          if (this.idbTransaction != null)
              this.idbTransaction.Rollback();
          idbTransaction = null;
      }
  
  
  
      /// &lt;summary&gt;
      /// Executes the reader.
      /// &lt;/summary&gt;
      /// &lt;param name="commandType"&gt;Type of the command.&lt;/param&gt;
      /// &lt;param name="commandText"&gt;The command text.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public IDataReader ExecuteReader(CommandType commandType, string commandText)
      {
          this.idbCommand = DBManagerFactory.GetCommand(this.ProviderType);
          idbCommand.Connection = this.Connection;
          PrepareCommand(idbCommand, this.Connection, this.Transaction, commandType, commandText, this.Parameters);
          this.DataReader = idbCommand.ExecuteReader();
          idbCommand.Parameters.Clear();
          return this.DataReader;
      }
  
      /// &lt;summary&gt;
      /// Closes the reader.
      /// &lt;/summary&gt;
      public void CloseReader()
      {
          if (this.DataReader != null)
              this.DataReader.Close();
      }
  
      private void AttachParameters(IDbCommand command, IDbDataParameter[] commandParameters)
      {
          foreach (IDbDataParameter idbParameter in commandParameters)
          {
              if ((idbParameter.Direction == ParameterDirection.InputOutput) &amp;&amp; (idbParameter.Value == null))
              {
                  idbParameter.Value = DBNull.Value;
              }
              command.Parameters.Add(idbParameter);
          }
      }
  
      private void PrepareCommand(IDbCommand command, IDbConnection connection, IDbTransaction transaction, CommandType commandType, string commandText, IDbDataParameter[] commandParameters)
      {
          command.Connection = connection;
          command.CommandText = commandText;
          command.CommandType = commandType;
  
          if (transaction != null)
          {
              command.Transaction = transaction;
          }
  
          if (commandParameters != null)
          {
              AttachParameters(command, commandParameters);
          }
      }
  
      /// &lt;summary&gt;
      /// Executes the command and returns the number of rows affected as an int.
      /// &lt;/summary&gt;
      /// &lt;param name="CommandType"&gt;Type of the command.&lt;/param&gt;
      /// &lt;param name="CommandText"&gt;The command text.&lt;/param&gt;
      /// &lt;returns&gt;Returns the number of rows affected.&lt;/returns&gt;
      public int ExecuteNonQuery(CommandType CommandType, string CommandText)
      {
          this.idbCommand = DBManagerFactory.GetCommand(this.ProviderType);
          PrepareCommand(idbCommand, this.Connection, this.Transaction, CommandType, CommandText, this.Parameters);
          int returnValue = idbCommand.ExecuteNonQuery();
          idbCommand.Parameters.Clear();
          return returnValue;
      }
  
      /// &lt;summary&gt;
      /// Executes the command and returns the results as an object.
      /// &lt;/summary&gt;
      /// &lt;param name="CommandType"&gt;Type of the command.&lt;/param&gt;
      /// &lt;param name="CommandText"&gt;The command text.&lt;/param&gt;
      /// &lt;returns&gt;An object as returned by the command.&lt;/returns&gt;
      public object ExecuteScalar(CommandType CommandType, string CommandText)
      {
          this.idbCommand = DBManagerFactory.GetCommand(this.ProviderType);
          PrepareCommand(idbCommand, this.Connection, this.Transaction, CommandType, CommandText, this.Parameters);
          object returnValue = idbCommand.ExecuteScalar();
          idbCommand.Parameters.Clear();
          return returnValue;
      }
  
      /// &lt;summary&gt;
      /// Executes the data set.
      /// &lt;/summary&gt;
      /// &lt;param name="commandType"&gt;Type of the command.&lt;/param&gt;
      /// &lt;param name="commandText"&gt;The command text.&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public DataSet ExecuteDataSet(CommandType commandType, string commandText)
      {
          this.idbCommand = DBManagerFactory.GetCommand(this.ProviderType);
          PrepareCommand(idbCommand, this.Connection, this.Transaction, commandType, commandText, this.Parameters);
          IDbDataAdapter dataAdapter = DBManagerFactory.GetDataAdapter(this.ProviderType);
          dataAdapter.SelectCommand = idbCommand;
          DataSet dataSet = new DataSet();
          dataAdapter.Fill(dataSet);
          idbCommand.Parameters.Clear();
          return dataSet;
      }
  
      public DataTable ExecuteDataTable(CommandType commandType, string commandText)
      {
          this.idbCommand = DBManagerFactory.GetCommand(this.ProviderType);
          PrepareCommand(idbCommand, this.Connection, this.Transaction, commandType, commandText, this.Parameters);
          //IDbDataAdapter dataAdapter = DBManagerFactory.GetDataAdapter(this.ProviderType);
          //dataAdapter.SelectCommand = idbCommand;
  
          DataTable dataTable = new DataTable();
          using (IDataReader idr = ExecuteReader(commandType, commandText))
          {
              dataTable.Load(idr);
          }
  
  
          idbCommand.Parameters.Clear();
          return dataTable;
      }
  
  }
  </code></pre>