Config database string read, modify

Source: Internet
Author: User
Tags connectionstrings

Config database string:

<connectionStrings>
<add name= "mywebdatastring" connectionstring= "server=192.168.0.4;database=clwmsdb;uid=sa;pwd=******"/>
</connectionStrings>


Read string (2 methods):

(1) Add system.web reference
string connctionstring = system.web.configuration.webconfigurationmanager.connectionstrings["connectionString "]. ToString ();


(2) Add system.configuration reference
string connctionstring = system.configuration.configurationmanager.connectionstrings["connectionString"]. ToString ();

To read a single item:
SqlConnection con =new SqlConnection (connctionstring);
con. Open ();
string Aserver=con. datasource;//Read Server 192.168.0.4
string asql = con. database;//Read Database Clwmsdb
Description:
(1) The above method does not know why only the server address and database can be read, not to read the user name and password .
(2) The method of reading the user name and password is as follows (a stupid method, that is, to split the string ):
string[] Constr = Connctionstring.split (';');
string[] struser = constr[2]. Split (' = ');
string UserId = Struser[1].  Trim (); User name
//Password strpassword[1]. Trim ()
string[] strpassword = constr[3]. Split (' = ');
txtpassword.textmode = Textboxmode.password;
this.txtPassword.Attributes.Add ("value", strpassword[1]. Trim ());

Note: If the form is in a password box, it is best to add this.txtPassword.Attributes.Add ("value", strpassword[1]. Trim ()); Otherwise, the password is not displayed;


To Modify a string:

Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ( Request.applicationpath);
connectionstringssection consection = (connectionstringssection) config. GetSection ("connectionString s");
//modified string
string newstr = "server=" + txtServer.Text.Trim () + ";d atabase=" + txtSql.Text.Trim () + "; uid=" + Txtuser.text . Trim () + ";p wd=" + TxtPassword.Text.Trim ();
consection.connectionstrings["Mywebdatastring"]. ConnectionString = Newstr;
CONFIG. Save ();
SqlConnection conn= New SqlConnection (NEWSTR);
//Verify connectivity to the database
if (con. state = ConnectionState.Open) {
try {
con. Open ();
} catch {
Scriptmanager.registerstartupscript (this, GetType (), "AA", "alert (' Save succeeded!") But the configuration is not valid! ') ", true);
return;
            }
        }

Config database string read, modify

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.