I am trying to create a connection pool in C# but keep getting the following error message: The ConnectionString property has not been initialized.
I understand that his is because I am accessing the variable ConnectionString
in the try-catch, is there any other way for me to tackle this?
MainWindow.cs
public MainWindow()
{
InitializeComponent();
using (var connection = new SqlConnection(DbConnect.ConnectionString))
{
// Reset plot list
connection.Open();
ResetPlotList(_filterPlotReference);
ResetWatchList(_operatorId);
ResetPositionList(_operatorId);
connection.Close();
}
DbConnect.cs
public class DbConnect
{
public static string ConnectionString = null;
public void Initialize()
{
try
{
XDocument doc = XDocument.Load("DbConnect.xml");
Dictionary<string, string> values =
doc.XPathSelectElements("//Connection[@name='default']")
.Single()
.Elements("Attribute")
.ToDictionary(el => (string) el.Attribute("name"), el => (string) el.Attribute("value"));
_server = values["server"];
_database = values["database"];
_uid = values["uid"];
_password = values["password"];
Plot = values["plot"];
ConnectionString = "SERVER=" + _server + ";" + "DATABASE=" +
_database + ";" + "UID=" + _uid + ";" + "PASSWORD=" + _password;
Connection = new MySqlConnection(ConnectionString);
}
catch (FileNotFoundException)
{
Environment.Exit(0);
}
}
}