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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:29:01+00:00 2026-05-29T06:29:01+00:00

This one is tough to explain inside the title. What I have going on

  • 0

This one is tough to explain inside the title. What I have going on is a foreach loop that sets a new connection string for each target server and database. However, when the foreach loop comes around, it sets the string as intended however, the SqlConnection remains looking at the the first string.

Below is what I’m using to call the connection:

 foreach (string DatabaseConfig in DataHoldingClass.Server_Database_Config)
            {
                string[] splitConfig = DatabaseConfig.Split('|');

                //set sql connection config
                DataHoldingClass.Database = splitConfig[0];
                DataHoldingClass.SQLSERVER = splitConfig[1];

                //open specific config


                SQLProcessorClass.SQLMASTERCONNECTION =  SQLProcessorClass.OpenSQLConnection();

                //do some work here
                SQLProcessorClass.SQLMASTERCONNECTION.Dispose();
}

Below is what i’m using to set the connection:

public static string masterConString = "server="+DataHoldingClass.SQLSERVER+";database ="+DataHoldingClass.Database+";Trusted_Connection=Yes;persist security info=False;connection timeout=500";

    public static SqlConnection SQLMASTERCONNECTION { get; set; }

    public static SqlConnection OpenSQLConnection()
    {
        SqlConnection sqlCon = new SqlConnection(masterConString);
        sqlCon.Open();
        return sqlCon;
    }

And below is DataHoldingClass

  public static string Database { get; set; }
    public static string SQLSERVER { get; set; }

so for example my string DatabaseConfig = “Database1|Server1” and the next loop in the DataHoldClass.Server_Database_Config is “Database2|Server2”, when the loop happens the second time around it continues using “Database1|Server1” even though it’s on the second string of the loop.

Any ideas?

Oh, I’m not actually calling the Database function even though stackoverflow is highlight that in blue.

  • 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-05-29T06:29:02+00:00Added an answer on May 29, 2026 at 6:29 am

    The code you posted does not call a getter to change the static string masterConString, so will use the one created the first time you create a SQLProcessorClass object and remain set to that.

    You could simplify to:

    foreach (string DatabaseConfig in DataHoldingClass.Server_Database_Config) 
    { 
        string[] splitConfig = DatabaseConfig.Split('|');
        string connectionString = "server=" + splitConfig[1] + ";database =" + splitConfig[0] +
                   ";Trusted_Connection=Yes;persist security info=False;connection timeout=500";
    
        using (SqlConnection cnn = new SqlConnection(connectionString))
        {
            cnn.Open();
    
            // do work....
    
        }
    }
    

    Perhaps add try/catch and some checking such as splitConfig.Length >= 2

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

Sidebar

Related Questions

This one's a tough one - I have a JFrame that generates JTextFields. When
This is driving me nuts. Its a tough one to explain but I'll have
This one's tough to explain, so I'll try to show what I'm after using
This is tough one to explain so i'll try my best, and hopefully edit
Its a bit of a tough one to explain really. this is it: <div
This one has me beat; I have a WPF window with two (important for
Anyhow, I have a problem. A tiny problem I suppose, but one that's bugging
This is a straightforward problem to explain and yet I'm having some tough time
So I know javascript pretty well, but I'm not sure about this one. Tough
this one is hard to explain! I am writing a python application to be

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.