Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:37:16+00:00 2026-06-13T11:37:16+00:00

Background: I am making a c# 4.0 class for re-use so I can send

  • 0

Background:

I am making a c# 4.0 class for re-use so I can send one parameter to most methods of my program and pass a ‘Connections’ object that can hold things like:

  • ConnectionStrings
  • DbConnection (i.e. OleDbConnection or SQLConnection)
  • DataReader (i.e. OleDbDataReader or SQLDataReader)

The reasoning is that I will eventually upgrade to SQL Server and don’t want to change my method parameters or code so much. Methods often have to call a quick query here and there from the same database.

Question:

How do I properly setup (and call) the right-hand side of the DRMDbConnection ‘set’ parameter for DbType (now hard-coded as integer 1) in the method below at line:

set { drmDbConnection = SetDRMDbConnection(1); }

Sample Code:

public class Connections
{

public string DRMDbConnectionString { get; set; }
private object drmDbConnection;

public object DRMDbConnection
{
  get { return drmDbConnection; }
  set { drmDbConnection = SetDRMDbConnection(1); }
}

private object SetDRMDbConnection(int DbType)
{
  if (DbType == 1)
    return new System.Data.OleDb.OleDbConnection();
  else
    return new SqlConnection();
}

}

EDITED Oct 31

I updated the class code to use the recommended IDbConnection, IDbCommand and IDataReader. Note: without a change to the SQL Server connection string you will recieve an error: “There is already an open DataReader associated with this Command which must be closed first.” Thus add this to connection string: “MultipleActiveResultSets=True;”

Updated Class code

public class Connections
// reusuable object to hold all database Connections and a convenient DataReader and Command object.
// Its purpose is to also be able to handle your choice of database connection, i.e. OleDb or SQL Server.
{
public string DRMConnectionString { get; set; }
public IDbConnection DRMConnection;

public IDbCommand DRMCommand
{
  get { return DRMConnection.CreateCommand(); }
  set { }
}

public int DRMConnectionType
// must be called to setup database connection and command.  The connectionstring must be previously set.
{
  set
  {
    if (value == 1)
      DRMConnection = new System.Data.OleDb.OleDbConnection(DRMConnectionString);
    else
      DRMConnection = new SqlConnection(DRMConnectionString);
  }
}

public void CloseConnections()
{
  if (DRMCommand != null)
    DRMCommand.Dispose();

  if ((DRMConnection != null) && (DRMConnection.State != ConnectionState.Closed))
    DRMConnection.Close();
}
}

CALLING CODE

var conn = new Connections();
conn.DRMConnectionString = "my connection string";
conn.DRMConnectionType = 2;
conn.DRMConnection.Open();
try
{
  using (var cmd = conn.DRMCommand)
  {
    cmd.CommandText = "SELECT * FROM MyTable";
    using (var rdr = cmd.ExecuteReader())
    {
      while (rdr.Read())      
      {
         CallSubMethodThatAlsoOpensDataReader(conn);  
      } 
    } 
  }
}
finally
{
  conn.CloseConnections();
} 


CallSubMethodThatAlsoOpensDataReader(Connections Conn)
{
  using (var cmd = Conn.DRMCommand)
  {
    cmd.CommandText = "SELECT * FROM MyOtherTable";
    using (var rdr = cmd.ExecuteReader())  // Error: There is already an open DataReader associated with this Command which must be closed first.
    {
       ; // do stuff
    } 
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:37:17+00:00Added an answer on June 13, 2026 at 11:37 am

    You can make it work as follows:

    public IDbConnection DRMDbConnection // Note the change of the type
    {
        get { return drmDbConnection; }
    }
    public int DrmDbConnectionType
    {
        set {
            if (vale== 1)
                drmDbConnection = new System.Data.OleDb.OleDbConnection();
            else
                drmDbConnection = new SqlConnection();
        }
    }
    

    The reasoning is that I will eventually upgrade to SQL Server and don’t want to change my method parameters or code so much.

    I think this is the key issue that you are trying to solve. Rather than programming to the specific class, program to an interface. Specifically, using IDbConnection instead of SqlConnection or OleDbConnection would let you hide the exact type better than encapsulating it in the DRMDbConnection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a handler class that will read similar to this: package avian.background {
Background: I'm acquireing cookies through my app and use them internally by making requests
I have a website that I'm making that needs a background resizer. Currently, I
Can anyone see where I could be making a mistake? The form text-box background
I pass a data access class (DAL) of my own making into a another
I make some game, I use I lot of graphics. I make class that
I am making an android application that needs to use a ListView . I
I want to set the background color of JButton. For that I use setBackground()
I am making an app for my class final that uses a GridView in
I am making a top-down shooter that makes extensive use of TMX maps created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.