vs in C # Three ways to read the app. Config database configuration string (GO)

Source: Internet
Author: User
Tags connectionstrings

vs in C # Three ways to read the app. Config database configuration string (GO) http://hi.baidu.com/mindox/item/3278dc352c7ba68fb80c0389http//www.blogjava.net/keweibo/articles/391207.htmlthree 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, the string will be used when you use the dataset. Sqldataaparter,sqlconnection, such as the shadow prompts you to choose, or a new string. There are three ways to get this string in code: The 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 it is 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, Initial catalog= database name; User Id=sa; Password= your own code."ProviderName="System.Data.SqlClient"/> <add name="testconnectionstring"connectionstring="data source= your database, Initial catalog= database name; User Id=sa; Password= your own code."ProviderName="System.Data.SqlClient"/> </connectionStrings></configuration>Description: Where the1"<appSettings> <add key="ConnectionString"Value="Data source= your database (localhost available, if it is 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. The2"<add name="SxzzManager.Properties.Settings.sxzzConnectionString"connectionString="data source= your database, Initial catalog= database name; User Id=sa; Password= your own code."ProviderName="System.Data.SqlClient"/>The content that is automatically generated when you add a data source in VS2008. The3"<add name="testconnectionstring"connectionstring="data source= your database, Initial catalog= database name; User Id=sa; Password= your own code."ProviderName="System.Data.SqlClient"/>added for manual. To obtain the1The connection string shown in the following statement:stringconstring = system.configuration.configurationsettings.appsettings["ConnectionString"]; stringconstring = system.configuration.configurationmanager.appsettings["ConnectionString"], to obtain the2There are two ways to connect the string shown in<1:stringConstr = system.configuration.configurationmanager.connectionstrings["SxzzManager.Properties.Settings.sxzzConnectionString"].           ToString (); <2:stringConstr = 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 the string that follows the last point in "SxzzManager.Properties.Settings.sxzzConnectionString". To obtain the3The connection string shown in the "2a string in the "stringConstr = configurationmanager.connectionstrings["testconnectionstring"].           ToString (); stringConstr = configurationmanager.connectionstrings["testconnectionstring"]. ConnectionString: If vs does not recognize the ConfigurationManager class, you need to add a "system.configuration" reference to the reference

vs in C # Three ways to read the app. Config database configuration string (GO)

Related Article

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.