Some basics about the app configuration file

Source: Internet
Author: User
Tags connectionstrings

Three ways to configure a database configuration string in VS2008 or VS2005
VS2008 When you create a form program, if you add a data source that automatically writes the connection string in the config file App. Config, this string will be used when you use Dataset,sqldataaparter, SqlConnection, such as the shadow prompts you to choose, or a new string. If you want to get this string in code, there are three ways:

App. Config content:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key= "ConnectionString" value= "Data source= your database (localhost available, if the Express development version must be. \sqlexpress); Initial catalog= database name; User id= username; Password= your own password/>
<add key= "TemplatePath" value= "Template"/>
</appSettings>
<connectionStrings>
<add name= "SxzzManager.Properties.Settings.sxzzConnectionString"
connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
<add name= "testconnectionstring" connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Description
which
"1"
< appsettings>
<add key= "ConnectionString" value= "Data source= your database (localhost available, if the Express development version must be. \sqlexpress); Initial catalog= database name; User id= username; Password= your own password/>
<add key= "TemplatePath" value= "Template"/>
</appSettings>
To be added by hand.
"2"
<add name= "SxzzManager.Properties.Settings.sxzzConnectionString"
connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
The content that is automatically generated when you add a data source in VS2008.
"3"
<add name= "testconnectionstring" connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
Added for manual.
To obtain the connection string shown in "1", use the following statement:
String constring = system.configuration.configurationsettings.appsettings["ConnectionString"];

String constring = system.configuration.configurationmanager.appsettings["ConnectionString"];

There are two ways to get the connection string shown in "2"
<1>: string constr = system.configuration.configurationmanager.connectionstrings["SxzzManager.Properties.Settings.sxzzConnectionString"]. ToString ();
<2>: string constr = sxzzmanager.properties.settings.default["sxzzconnectionstring"]. ToString ();//"where" sxzzconnectionstring "can be displayed in the dialog box that pops up in the VS2008 menu =" Project = "Property =" set = "name. is actually "SxzzManager.Properties.Settings.sxzzConnectionStringString after the last point in the.
To get the connection string shown in "3", a string in "2" can be used
String constr = configurationmanager.connectionstrings["testconnectionstring"]. ToString ();
String constr = configurationmanager.connectionstrings["testconnectionstring"]. ConnectionString;

If it appears: Configuration system failed to initialize error, turn configurationstring into appseting

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.