Esempio n. 1
0
        private static void ChangeConnectionString()
        {
            LocalDataHandler ldh = new LocalDataHandler();;

            AppDomain.CurrentDomain.SetData("DataDirectory", ldh.GetDataDirectory());
            //try
            //{
            //    XDocument doc = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            //    var query = from p in doc.Descendants("connectionStrings").Descendants()
            //                select p;
            //    foreach (var child in query)
            //    {
            //        foreach (var atr in child.Attributes())
            //        {
            //            if (atr.NextAttribute != null && atr.NextAttribute.Name == "connectionString")
            //            {
            //                Dictionary<string, string> dictionary = new Dictionary<string, string>();
            //                string[] items = atr.NextAttribute.Value.TrimEnd(';').Split(';');
            //                foreach (string item in items)
            //                {
            //                    if (item.Contains("="))
            //                    {
            //                        string[] keyValue = item.Split('=');
            //                        dictionary.Add(keyValue[0], keyValue[1]);
            //                    }
            //                }
            //                SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
            //                connectionStringBuilder.AttachDBFilename = @"C:\Users\aidan\source\repos\CaseStudy4\GitHub\AppUpdated\DataHandler\LocalDb.mdf;";
            //                connectionStringBuilder.ApplicationName = "LocalDbEntities";
            //                //connectionStringBuilder. = dictionary["provider"];
            //                //connectionStringBuilder. = dictionary["metadata"];
            //                //connectionStringBuilder["provider connection string"] = dictionary["provider connection string"];
            //                connectionStringBuilder.DataSource = @"(LocalDB)\MSSQLLocalDB;";
            //                connectionStringBuilder.MultipleActiveResultSets = true;
            //                connectionStringBuilder.IntegratedSecurity = true;


            //                EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(atr.NextAttribute.Value);
            //                entityBuilder.ProviderConnectionString = connectionStringBuilder.ToString();
            //                atr.NextAttribute.Value = entityBuilder.ToString();

            //            }
            //        }

            //    }
            //    doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            //    ConfigurationManager.RefreshSection("connectionString");
            //}
            //catch (Exception x)
            //{
            //}
        }