简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM