Enterprise Library custom Config File

Source: Internet
Author: User

We all know how to write the configuration information of the Enterprise Library in Web. config and App. config. The following describes how to manage the configuration information independently, and how to name and place the config file at will.

In general, we write the following in the config file:

< Configuration >
< Configsections >
< Section Name = "Dataconfiguration" Type = "Microsoft. Practices. enterpriselibrary. Data. configuration. databasesettings, Microsoft. Practices. enterpriselibrary. Data"   />
</ Configsections >
< Connectionstrings >
< Add
Name = "Northwind"  
Providername = "System. Data. sqlclient"
Connectionstring = "Data Source = (local); initial catalog = northwind; Integrated Security = true"   />
Connectionstrings>
< Dataconfiguration Defaultdatabase = "Northwind" />
</ Configuration >

TheCode:

Database northwind = Databasefactory. createdatabase ();
Database northwind = Databasefactory. createdatabase ( " Northwind " );

In fact, you can also achieve this:
Or the above config content, but you can name this config file at will and save it to another path. When creating a database instance, write as follows: Iconfigurationsource Source =
New Fileconfigurationsource ( " My. config " );

Databaseproviderfactory Factory =
New Databaseproviderfactory (source );

Database northwind = Factory. Create ( " Northwind " );

In addition, we can use the original code block to create a database instance based on a custom config file.

Write in APP. config and web. config as follows:

< Configuration >
< Configsections >
< Section
Name = "Enterpriselibrary. configurationsource"
Type = "Microsoft. Practices. enterpriselibrary. Common. configuration. configurationsourcesection, Microsoft. Practices. enterpriselibrary. Common"   />
</ Configsections >
< Enterpriselibrary . Configurationsource selectedsource = "Filesource" >
< Sources >
< Add
Name = "Filesource"
Type = "Microsoft. Practices. enterpriselibrary. Common. configuration. fileconfigurationsource, Microsoft. Practices. enterpriselibrary. Common"
Filepath = "My. config"   />
< Add
Name = "Systemsource"
Type = "Microsoft. Practices. enterpriselibrary. Common. configuration. systemconfigurationsource, Microsoft. Practices. enterpriselibrary. Common"   />
</ Sources >
</ Enterpriselibrary. configurationsource >
</ Configuration >

The database connection configuration is still in my. config, and the code can use the original method to create an instance:Database northwind=Databasefactory. createdatabase ();
Database northwind=Databasefactory. createdatabase ("Northwind");

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.