我正在使用以下代码来设置连接字符串:
ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder ();
builder.ConfigureData ()
.ForDatabaseNamed ( "TestDatabase" )
.ThatIs.ASqlDatabase ()
.WithConnectionString ( "Data Source=127.0.0.1;User Id=sa;Password=123;Initial Catalog=DataAccessExamples;" )
.AsDefault ();
var configSource = new DictionaryConfigurationSource ();
builder.UpdateConfigurationWithReplace ( configSource );
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer ( configSource );
执行上述代码后,我希望以下代码可以工作,但它说连接未初始化并且 command.Connection 属性始终保持为空。
Database database = DatabaseFactory.CreateDatabase ();
DbCommand command = database.GetSqlStringCommand ( "Select * from TT" );
DbDataReader dbReader = command.ExecuteReader ( System.Data.CommandBehavior.CloseConnection );
while (dbReader.Read ())
{
System.Diagnostics.Debug.WriteLine ( dbReader[0].ToString () );
}
请谁能告诉我为什么连接没有被初始化?
我在库项目中使用上述代码,并希望在运行时配置 DAAB。
谢谢