Clumsy birds have essay: ADO about C # using the database connection string from app. Config

Source: Internet
Author: User
Tags connectionstrings

The application configuration file is a standard XML file, and XML tags and attributes are case-sensitive. It can be changed as needed, and developers can use the configuration file to change the settings without having to recompile the application. In the app. config file, the root node is the configuration, and the node under the root node that can create the connection string, which is connectionstrings.

I. Configuration method for app. Config

1. Add an application configuration file (App. config) to the project, add the connection string node under the root node <connectionStrings></connectionStrings>

2. In the connection string node connectionstrings, you can add a connection string with the following code syntax:

Note: You can add multiple connection strings here, and you can choose to use different database connection strings as needed in your project

Differentiate with the name attribute

<add name= "Constr" connectionstring= "server=local;database=teaching;uid=sa;pwd=123456"/>

<add name= "CONSTR2" connectionstring= "server=locak;database=entityframework;uid=sa;pwd=123"/>

Two. About the use of App. Config in C #

The detailed code is as follows:

Call the database connection string in app. Config to summarize a sentence convenient to remember, do not laugh ~ ~ ~

(ConfigurationManager) The profile administrator selects the connectionstrings (connection string) node under the name XXX database and obtains the connection word assigned to the defined Constr.

String constr = configurationmanager.connectionstrings["Constr"]. ConnectionString;

Instantiates a connection object and puts the obtained string constr into the constructor

using (SqlConnection conn = new SqlConnection (CONSTRR))
{

Open connection
Conn. Open ();
}

Clumsy birds have essay: ADO about C # using the database connection string from app. Config

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.