0

次のコードを使用して接続文字列をセットアップしています。

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 を構成したいと考えています。

ありがとう

4

1 に答える 1

0

1 つには、なぜ接続文字列をハード コーディングするのでしょうか。アプリケーションの構成ファイルに接続文字列を含める方が理にかなっていると思いませんか?

データベース ファクトリを使用する必要があるのはなぜですか?

        SqlDatabase db = new SqlDatabase("some connection string from the config");
        DbCommand cmd = db.GetSqlStringCommand(sqlStatement);
        IDataReader reader = db.ExecuteReader(cmd);
于 2011-05-02T14:09:39.847 に答える