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");