【问题标题】:Data Access Application Block 5.0 fluent configurationData Access Application Block 5.0 流畅配置
【发布时间】:2011-08-17 00:14:45
【问题描述】:

我正在使用以下代码来设置连接字符串:

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。

谢谢

【问题讨论】:

    标签: c# configuration fluent daab


    【解决方案1】:

    首先,为什么要对连接字符串进行硬编码?在您的应用程序的配置文件中包含连接字符串不是更有意义吗?

    为什么需要使用数据库工厂?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 2010-10-12
      • 1970-01-01
      相关资源
      最近更新 更多