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 209927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:57:34+00:00 2026-05-11T17:57:34+00:00

I’m in the process of adding a Data Access Layer for our ASP.Net 2.0

  • 0

I’m in the process of adding a Data Access Layer for our ASP.Net 2.0 web application that was written almost exclusively using in-line SQL calls and copious use of copy/paste. Due to business concerns I am only able to re-factor small portions of the application at a time (can’t settle in and do a huge re-design), so I have to live with certain design decisions until things are loosely coupled enough to make big changes and test them properly.

I’ve decided to use the Enterprise Library Data Access Application Block as the plumbing for the Data Access Layer going forward, and one such previous design decision is causing me problems. Currently we get the “main” connection string for our application from an admin database based on the account id provided by the user so that we can allow a single installation of the application to access multiple databases. My problem deals with trying to figure out the best practices way of getting that connection string (or the account id) to the DAL.

The previous implementation was storing the encrypted connection string in a cookie, so my current hack-approach is to get the connection string from there and then use DAAB in the following manner:

Public Shared Function GetResultsByKeywords(ByVal key1 As String, ByVal key2 As String, ByVal key3 As String, ByVal key4 As String) As DataTable
    Dim db As SqlDatabase = New SqlDatabase(Connection.GetConnectString())
    Dim dt As DataTable = New DataTable

    Using dr As IDataReader = db.ExecuteReader("sel_eHELP_ITEMS", key1, key2, key3, key4)
        dt.Load(dr)
    End Using

    Return dt
End Function

Where Connection.GetConnectString() is fetching the connection string from the cookie.

I know this is nowhere near the best way to do this and I need to fix it, but I’m struggling with the “right” way to get it done. Maybe create a base class that I can initialize with the connection string and inherit all other classes from, but I’m not sure how this would look and where I would first initialize an instance of that class. On every page before accessing any DAL functions?

Any guidance would be appreciated. Thank you.

Update

Let me clarify. At most there will be 3 or 4 different connection strings for an installation. The “account id” is not a user id and is used by multiple users to essentially specify which database to connect to.

  • 1 1 Answer
  • 1 View
  • 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-05-11T17:57:35+00:00Added an answer on May 11, 2026 at 5:57 pm

    Edit:

    With your latest update, I still recommend looking into the Configuration using the DAAB
    http://davidhayden.com/blog/dave/archive/2006/01/23/2744.aspx

    I’m having difficulty finding/opening the Ent Lib docs, but I would see if it has the equivalent of ChangeDatabase method of the SqlConnection object. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.changedatabase.aspx

    Here’s one way to do it .. Since I don’t think your DAL should know about your switching DB requirement I would setup your DAL with a ConnectionString as a parameter of the constructor. Use a wrapper method/property to hide/encapsulate that requirement and get the instance of the DAL you would like to use in your app.

    (please excuse the c#)

    public class MyDAL
    {
        private string _connectionString;
    
        public MyDAL(string connectionString)
        {
            _connectionString = connectionString;
        }
    
        public int MyDatabaseCall()
        {
            using (SqlConnection conn = new SqlConnection(_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("my sql", conn))
                {
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        //...data access
                        return 0;
                    }
                }
            }
        }
    }
    
    public class MyApp
    {
        public static Dictionary<string, MyDAL> myDatabases = new Dictionary<string, MyDAL>();
    
        public string GetConnectionString(string database)
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[database].ConnectionString;
        }
    
        public void StartUp() // Global.asax Application_Start ?
        {
            myDatabases.Add("Database1", new MyDAL(GetConnectionString("Database1")));
            myDatabases.Add("Database2", new MyDAL(GetConnectionString("Database2")));
            myDatabases.Add("Database3", new MyDAL(GetConnectionString("Database3")));
        }
    
        public MyDAL DataAcccessLayer
        {
            get
            {
                string usersDB = FigureOutUsersDatabase();
                return myDatabases[usersDB];
            }
        }
    
        public void UseSomeData()
        {
            var myData = DataAcccessLayer.MyDatabaseCall();
            //Do Stuff
        }
    }
    

    Hope that helps, good luck! And be very careful what you put in cookies. 😉

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

Sidebar

Ask A Question

Stats

  • Questions 144k
  • Answers 144k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I honestly would create a Column on the table of… May 12, 2026 at 8:39 am
  • Editorial Team
    Editorial Team added an answer I found why this morning: In web.config, under globalization, the… May 12, 2026 at 8:39 am
  • Editorial Team
    Editorial Team added an answer Range.Font.Color = wdColorRed http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2006-08/msg00064.html May 12, 2026 at 8:39 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.