Set the connection string in ASP. NET 2.0

Source: Internet
Author: User

In ASP. NET 2.0, a new declarative expression syntax is used to parse a connection string value at runtime, referencing the database connection string by name. The connection string is stored under the <connectionStrings> Configuration section in the Web. config file to facilitate maintenance of all pages in the application at a single location.


The sample code is as follows: <? Xml version = "1.0"?> <Configuration> <connectionStrings> <add name = "Pubs" connectionString = "Server = localhost; Integrated Security = True; Database = pubs; Persist Security Info = True" providerName = "System. data. sqlClient "/> <add name =" Northwind "connectionString =" Server = localhost; Integrated Security = True; Database = Northwind; Persist Security Info = True "providerName =" System. data. sqlClient "/> </connectionStrings> <system. web> <pages styleSheetTheme = "Default"/> </system. web> </configuration>

Program Code Description: In the program code of the preceding example. under the <connectionStrings> Configuration node in the Config file, two database connection strings are set, pointing to the pubs and Northwind sample databases respectively. Note that a data source control is introduced in 2.0, such as the SqlDataSource control. We can set the ConnectionString attribute of the SqlDataSource control to an expression <% $ ConnectionStrings: Pubs %>, this expression is run by ASP.. NET analyzer is parsed as a connection string. You can also specify an expression for the ProviderName attribute of SqlDataSource, for example, <% $ ConnectionStrings: Pubs. ProviderName %>. The specific usage and new features will be detailed in the subsequent sections. Now you have a basic understanding. Of course, we can also use the following method to directly read the database connection string from the configuration file. First, we need to reference the using System. Web. Configuration namespace, which contains classes used to set ASP. NET configurations. String connectionString = ConfigurationManager. ConnectionStrings ["Northwind"]. ConnectionString;

Program Code Description: In the program code of the preceding example, we can use ConnectionStrings ["Northwind"] to read the corresponding Northwind string. Similarly, you can use ConnectionStrings ["Pubs"] to read the corresponding Pubs string.

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.