This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22253|Reply: 0

[Source] C# reads and sets the connectionstring in app.config

[Copy link]
Posted on 5/9/2015 5:03:45 PM | | |

//第一种获取连接字符串的方法
Properties.Settings connset = Properties.Settings.Default;
constr = connset. ConnectionString;
     
         
//第二种获取连接字符串的方法
ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["ConnectionString"];
string str = settings. ConnectionString;
//第三种获取连接字符串的方法
XmlDocument doc = new XmlDocument();
doc. Load(".. //.. //App.config");
XmlNode root = doc. SelectSingleNode("configuration");
XmlNode node = root. SelectSingleNode("connectionStrings/add[@name=’ConnectionString’]");
XmlElement el = node as XmlElement;
string str = el. GetAttribute("connectionString");
//在程序中设置连接字符串
connset. ConnectionString = "Data Source=orcl; Persist Security Info=True; User ID=xhfoc; Password=foc; Unicode=True";
connset. Save();
To modify the Settings.settings.cs file under the Properties namespace using this method, add the following code:
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=orcl; Persist Security Info=True; User ID=xhfoc; Password=foc; Unicode=True")]
         
public string ConnectionString
{
     get
     {
         return ((string)(this["ConnectionString"]));
     }
     set
     {
         this["ConnectionString"] = value;
     }
}
              
Methods to modify the profile         
XmlDocument doc = new XmlDocument();
doc. Load(".. //.. //App.config");
XmlNode root = doc. SelectSingleNode("configuration");
XmlNode node = root. SelectSingleNode("connectionStrings/add[@name=’ConnectionString’]");
XmlElement el = node as XmlElement;
el. SetAttribute("connectionString", "Data Source=orcl; Persist Security Info=True; User ID=xhfoc; Password=foc; Unicode=True");
doc. Save(".. //.. //App.config");




Previous:Yesterday I did a wrong thing with linq
Next:The type "System.Data.Linq.DataContext" is defined in the unreferenced assembly. Must add a response to the program...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com