中为DBEntities更改RunTime中的connectio

中为DBEntities更改RunTime中的connectio

本文介绍了在app.config中为DBEntities更改RunTime中的connectionStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i希望在app.config中为DBEntities更改RunTime中的connectionStrings

i执行:

hii want to Change connectionStrings in RunTime in app.config for DBEntities
i do it :

<connectionStrings>
   <add name="NezamDBEntities" connectionString="metadata=res://*/Models.NezamDBModel.csdl|res://*/Models.NezamDBModel.ssdl|res://*/Models.NezamDBModel.msl;provider=System.Data.SqlClient;provider connection string='Server=MOHAMMADALI-PC;initial catalog=NezamDB;user id=sa;password=1;MultipleActiveResultSets=True;App=EntityFramework';" providerName="System.Data.EntityClient" />
   <add name="Server" connectionString="MOHAMMADALI-PC" />
   <add name="DataBase" connectionString="NezamDB" />
   <add name="User" connectionString="sa" />
   <add name="Pwd" connectionString="1" />
 </connectionStrings>




XmlDocument xmldoc = new XmlDocument();
               xmldoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
               XmlNode appsetting = xmldoc.SelectSingleNode("configuration/connectionStrings");
               string sbo = "metadata=res://*/Models.NezamDBModel.csdl|res://*/Models.NezamDBModel.ssdl|res://*/Models.NezamDBModel.msl;provider=System.Data.SqlClient;provider connection string=";
               sbo += "'Server=";
               sbo += txtServer.Text.Trim();
               sbo+=(";initial catalog=");
               sbo+=(txtDataBase.Text.Trim());
               sbo+=(";user id=");
               sbo+=(txtUser.Text);
               sbo+=(";password=");
               sbo+=(txtPassword.Text);
               sbo += (";MultipleActiveResultSets=True;App=EntityFramework';");
               foreach (XmlNode ChildNode in appsetting)
               {
                   if (ChildNode.Attributes["name"].Value == "NezamDBEntities")
                       ChildNode.Attributes["connectionString"].Value = sbo;
                   if (ChildNode.Attributes["name"].Value == "Server")
                       ChildNode.Attributes["connectionString"].Value = txtServer.Text;
                   if (ChildNode.Attributes["name"].Value == "DataBase")
                       ChildNode.Attributes["connectionString"].Value = txtDataBase.Text;//name data base
                   if (ChildNode.Attributes["name"].Value == "User")
                       ChildNode.Attributes["connectionString"].Value = txtUser.Text;//name user id
                   if (ChildNode.Attributes["name"].Value == "Pwd")
                       ChildNode.Attributes["connectionString"].Value = txtPassword.Text;//password bank

               }
               xmldoc.Save(AppDomain.CurrentDomain.BaseDirectory + @"..\..\App.config");
               xmldoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
               var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
               var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
               connectionStringsSection.ConnectionStrings["NezamDBEntities"].ConnectionString = sbo;
               config.Save(ConfigurationSaveMode.Modified);
               //ConfigurationManager.RefreshSection("connectionStrings");
               ConfigurationManager.RefreshSection(config.ConnectionStrings.SectionInformation.Name);
               Properties.Settings.Default.Reload();





但不工作



我该怎么办?



but notwork

what should i do ?

推荐答案


这篇关于在app.config中为DBEntities更改RunTime中的connectionStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:59