I have this SqlConnection in my forms that on initialization of forms I call as following:
public partial class options : Form
{
SqlConnection conn = new SqlConnection(myConnection.DataSource.ConnectionString);
This is how does look the class:
public class myConnection
{
internal static class DataSource
{
private static string _ConnectionString;
public static string ConnectionString
{
get
{
if (_ConnectionString == null)
_ConnectionString = FunctionToDynamicallyCreateConnectionstring();
return _ConnectionString;
}
}
private static string FunctionToDynamicallyCreateConnectionstring()
{
SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder();
cb.DataSource = Properties.Settings.Default.sql;
cb.InitialCatalog = Properties.Settings.Default.database;
cb.UserID = Properties.Settings.Default.user;
cb.Password = Properties.Settings.Default.pass;
cb.MultipleActiveResultSets = true;
cb.ConnectTimeout = 20;
return cb.ToString();
}}
In Options form in my project I do this on button click which changes the Settings.Settings values for particular string which are later used for SqlConnectionStringBuilder
Properties.Settings.Default.sql = sql.Text;
Properties.Settings.Default.Save();
Properties.Settings.Default.database = database.Text;
Properties.Settings.Default.Save();
Properties.Settings.Default.user = user.Text;
Properties.Settings.Default.Save();
Properties.Settings.Default.pass = pass.Text;
Properties.Settings.Default.Save();
And when I'm done with changes I close the form and on FormClosed event in Form1 from which was the options form raised I do this
conn = new SqlConnection(myConnection.DataSource.ConnectionString);
Which I thought that it would change the connection string according to actual values in Properties.Settings.Default.pass etc. strings. but it doesn't.
So my question, is there possibility to change somehow the string of already inialized SqlConnection ?
Thanks in advance for your time guys.