0

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);
            }
        }
   }
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • Why do you want to create your own connection pool? It is done for you unless you explicitly disable it (see, for example [here](http://stackoverflow.com/a/26089508/67392)). – Richard Feb 03 '15 at 10:42
  • Thanks Richard. Got a bit confused there. So would the `OpenConnection()` and `CloseConnection()` methods at http://pastebin.com/7R0XbKSN equate to creating a connection pool? – methuselah Feb 03 '15 at 10:45
  • The connection pool is part of ADO.NET, as soon as you dispose of a `DbConnect` instance (or rather of a type derived from it) it will go into the connection pool; but there is no operation you do that will directly create the connection pool: it exist implicitly. – Richard Feb 03 '15 at 11:04

0 Answers0