Microsoft.Practices.EnterpriseLibrary 2.0版本相对1.1版本有了很大的变化,很早前就下了1.1版,一直没在项目中用,看看gotDotNet,2.0版的CTP已经出了,下了个,用一下。。。。
Q:使用Microsoft.Practices.EnterpriseLibrary.Data,用默认的DatabaseFactory.CreateDatabase()方法创建对象实例时,调用的什么连接字符串?如何设置?
A:首先看看2.0的Microsoft.Practices.EnterpriseLibrary.Data的一些变化:
Data Access Application Block
Improvements in the System.Data namespace have enabled us to simplify the architecture of the Data Access Application Block. The main changes are as follows:
Internally, the application block now uses the new DbProviderFactory classes to create new database objects such as connections, commands and parameters.
The DbCommandWrapper and derived classes have been removed from the design. This is because the new classes in the System.Data.Common namespace have largely replaced the functionality of the DbCommandWrapper class. Certain methods that were on the DbCommandWrapper class have been moved to the Database class.
The block now uses System.Configuration to manage the database configuration information although the configuration data is structured in much the same way as it was in previous versions of Enterprise Library. It is likely that the final release will use the new built-in <configurationStrings> configuration section to store connection strings, rather than using its own configuration section. The block will still include its own configuration sections for advanced connection information such as Oracle package mapping.
It is now much easier to use the Data Access Application Block without any configuration. Connection strings can be passed directly to the constructors of the various database types, such as SqlDatabase.
Enterprise Library for .NET Framework 2.0 will not initially include support for IBM DB2 databases. This is because the new Data Access Application Block requires the use of ADO.NET 2.0-compliant managed providers, and none that support IBM DB2 databases are likely to be available in time for the final Enterprise Library release. The IBM DB2 databases may be supported in the future when ADO.NET 2.0 managed providers are available.
简单回到上面的问题,连接字符串的默认设置放在项目的配置文件中。如果是WinApp,那么配置文件就是“项目名.exe.config”(设计时可以设置App.config文件);如果是WebApp,那么配置文件是“web.config”。分别举例如下:
==========WinApp(App.config文件);========
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
</configSections>
<connectionStrings>
<add
name="DataAccessQuickStart1"
providerName="System.Data.SqlClient"
connectionString="server=(local);database=EntLibQuickStarts;Integrated Security=true" />
</connectionStrings>
<dataConfiguration defaultDatabase="DataAccessQuickStart1"/>
</configuration>
==========WebApp(web.config文件);=======
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
</configSections>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=.;Integrated Security=True;Initial Catalog=Morning2" providerName="System.Data.SqlClient"/>
</connectionStrings>
<dataConfiguration defaultDatabase="LocalSqlServer"/> <!--这一句给EnterpriseLibrary.Data的连接字符串用-->
<system.web>
<!--省略-->
</system.web>
</location>
</configuration>