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.
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 aSQLProcessorClassobject and remain set to that.You could simplify to:
Perhaps add try/catch and some checking such as
splitConfig.Length >= 2