简体   繁体   English

Data Access Application Block 5.0 流畅配置

[英]Data Access Application Block 5.0 fluent configuration

I am using following code to setup connection string:我正在使用以下代码来设置连接字符串:

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

After executing above code I expect following code to work but it says connection not initialized and command.Connection property always keeps null.执行上述代码后,我希望以下代码可以工作,但它说连接未初始化并且 command.Connection 属性始终保持 null。

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 () );
            }

Please can anyone tell me why connection is not getting initialized?请谁能告诉我为什么连接没有被初始化?

I am using above code in a library project and want to configure DAAB at runtime.我在库项目中使用上述代码,并希望在运行时配置 DAAB。

Thanks谢谢

For one, why are you hard coding the connection string?一方面,为什么要对连接字符串进行硬编码? Wouldn't it make more sense to have the connection string in the config file for your application?在您的应用程序的配置文件中包含连接字符串不是更有意义吗?

Why do you need to use the Database factory?为什么需要使用数据库工厂?

        SqlDatabase db = new SqlDatabase("some connection string from the config");
        DbCommand cmd = db.GetSqlStringCommand(sqlStatement);
        IDataReader reader = db.ExecuteReader(cmd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 配置Enterprise Library 5.0数据访问应用程序块 - Configuring Enterprise Library 5.0 Data Access Application Block 在应用程序配置文件中找不到Fluent Nhibernate和Crystal错误配置部分 - Fluent Nhibernate and quartz error configuration section not found in application configuration file 无法从.Net 5.0 WebApi 访问 GlobalConfiguration.Configuration - Can't access GlobalConfiguration.Configuration from .Net 5.0 WebApi 使用EL 5.0的日志应用程序块基于日期生成日志? - generate logs based on date using logging application block for EL 5.0? 将Fluent NHibernate与数据访问层配合使用 - Using Fluent NHibernate with a Data Access Layer 在.Net 5.0中配置服务时如何使用配置数据? - How to use configuration data when configuring services in .Net 5.0? Unity Application Block-配置文件中的构造方法注入 - Unity Application Block - Constructor injection in configuration file 如何使用企业库的配置应用程序块? - How to use configuration application block of enterprise library? 数据访问块中的加密 - Encryption in DATA access block Automapper 5.0全局配置 - Automapper 5.0 global configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM