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 160013

The Archive Base Latest Questions

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

I have a system that supports multiple products. Each product has its own database

  • 0

I have a system that supports multiple products. Each product has its own database with the same exact schema.

When I pass in the connection string as a parameter to my Data Context constructor it always uses the default database listed in the connection string, or the default database of the user connecting if I do not provide an Initial Catalog in the connection string.

I would like to be able to have the system utilize a database without having to change the connection string and by passing in the database name as a parameter.

Here is an example of the code I am using:

class Program     {         static void Main(string[] args)         {             var d = new Data('Data Source=(LOCAL);Initial Catalog=Database1;Integrated Security=true;');              var d1 = new Data('Data Source=(LOCAL);Initial Catalog=Database2;Integrated Security=true;');              Console.ReadLine();         }     }      internal class Data     {         public Data(string connection)         {              using (var ctx = new DataClassDataContext(connection))             {                 var query = from c in ctx.MyTable select c;                 try                 {                     Console.WriteLine(query.Count());                 }                 catch (Exception ex)                 {                     Console.WriteLine(ex.Message);                 }                }         }     } 

If this code gets executed, then the first result will pull from Database1 and the second result will pull from Database2. I would like it to have the ability to pull from a database that is not provided in the connection string. The reason for this is because the database could change based on a specific scenario but the connection string will remain the same.

Here is an example of what I am using to ‘fake’ it, but I don’t really think this is the best solution for this:

class oConnection     {         public string Server { get; set; }         public string Database { get; set; }         public bool IntegratedSecurity { get; set; }         public string UserName { get; set; }         public string Password { get; set; }     }      class Program     {         static void Main(string[] args)         {             var d = new Data(new oConnection                                  {                                      Database = 'Database1',                                      Server = '(Local)',                                      IntegratedSecurity = true                                  });              var d1 = new Data(new oConnection                                   {                                       Database = 'Database2',                                       Server = '(Local)',                                       IntegratedSecurity = true                                   });             Console.ReadLine();         }     }      internal class Data     {         private static string BuildConnection(oConnection connection)         {             var sb = new StringBuilder();             sb.Append('Data Source=' + connection.Server + ';Initial Catalog=' + connection.Database + ';');             if(connection.IntegratedSecurity)             {                 sb.Append('Integrated Security=true;');             }             else             {                 sb.Append('user id=' + connection.UserName + ';password=' + connection.Password);             }             return sb.ToString();         }          public Data(oConnection connection)         {              using (var ctx = new DataClassDataContext(BuildConnection(connection)))             {                 var query = from c in ctx.MyTable select c;                 try                 {                     Console.WriteLine(query.Count());                 }                 catch (Exception ex)                 {                     Console.WriteLine(ex.Message);                 }             }         }     } 

Another note: the goal of this is really to be able to support not having multiple different connection strings when running queries that will span across multiple databases. For example: If I want to query the account records from a database and then query some sort of lookup data from another database, I would have to create a new connection string for the context.

Any help would be appreciated. Thanks

  • 0 0 Answers
  • 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. 2026-05-11T10:57:36+00:00Added an answer on May 11, 2026 at 10:57 am

    Use the constructor that receives System.Data.IDbConnection connection. You can use the same connection string, and call connection.ChangeDatabase(‘mydb’) before passing it to the constructor. Alternatively you can add a new constructor on the partial class, so the calling call doesn’t has to deal with that.

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

Sidebar

Ask A Question

Stats

  • Questions 68k
  • Answers 68k
  • 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
  • added an answer First of all, the 'else if' statement is incorrect, since… May 11, 2026 at 12:18 pm
  • added an answer The problem is that, accessing an item in an array… May 11, 2026 at 12:18 pm
  • added an answer The feature is called covariance/contravariance and will be supported in… May 11, 2026 at 12:18 pm

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.