Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据访问应用程序块5.0 fluent配置_C#_Configuration_Fluent_Daab - Fatal编程技术网

C# 数据访问应用程序块5.0 fluent配置

C# 数据访问应用程序块5.0 fluent配置,c#,configuration,fluent,daab,C#,Configuration,Fluent,Daab,我正在使用以下代码设置连接字符串: ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder (); builder.ConfigureData () .ForDatabaseNamed ( "TestDatabase" ) .ThatIs.ASqlDatabase () .

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

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属性始终保持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 () );
            }
请告诉我为什么连接没有初始化

我正在库项目中使用上述代码,并希望在运行时配置DAAB


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

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

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