C # multiple methods to read and set connection strings

Source: Internet
Author: User

// Method 1 for obtaining the connection string
Properties. settings connset = properties. settings. default;
Constr = connset. connectionstring;

// Method 2 for obtaining the connection string
Connectionstringsettings settings = configurationmanager. connectionstrings ["connectionstring"];
String STR = settings. connectionstring;

// Method 3 for obtaining the connection string
Xmldocument Doc = new xmldocument ();
Doc. Load ("../.. // app. config ");
Xmlnode root = Doc. selectsinglenode ("configuration ");
Xmlnode node = root. selectsinglenode ("connectionstrings/Add [@ name = 'ononstring']");
Xmlelement El = node as xmlelement;
String STR = El. getattribute ("connectionstring ");

// In Program Set the connection string
Connset. connectionstring = "Data Source = orcl; persist Security info = true; user id = xhfoc; Password = FOC; Unicode = true ";
Connset. Save ();
To use this method, modify the settings. settings. CS file in the properties namespace and 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;
}
}


// Method for modifying the configuration file
Xmldocument Doc = new xmldocument ();
Doc. Load ("../.. // app. config ");
Xmlnode root = Doc. selectsinglenode ("configuration ");
Xmlnode node = root. selectsinglenode ("connectionstrings/Add [@ name = 'ononstring']");
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 ");

 

 

Turn: http://blog.csdn.net/lishiwenblog/archive/2007/07/22/1701851.aspx

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.