VS2005環境でのNAgile - アプリケーション設定はapp.configで変更可能 (^o^)
先日の日記でテストコードでTableAdapterの接続文字列を変更する方法を書いたが、もっとシンプルな方法があった。
アプリケーション設定で設定した接続文字列はapp.configのconnectionStringsセクションに落とし込まれている。つまりアプリケーション設定に埋め込まれた接続文字列は開発用サーバーのDBへの接続文字列である場合がほとんどだろうから、本番環境では適切にconfigファイルを書き換える必要があるということだろう。
そこでテストプロジェクトにapp.configを追加してTableAdapterのあるプロジェクトのapp.configのconnectionStringsセクションをコピーして変更する。
これだけで全てのTableAdapterはテスト用DBに接続しに行くようになる。
TableAdapterのあるプロジェクトのapp.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <!-- 開発用DBへの接続 --> <add name="Library.DataAccessLayer.Properties.Settings.LibraryConnectionString" connectionString="Data Source=BUILDSERVER;Initial Catalog=Library;Persist Security Info=True;User ID=sa;Password=" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
テストプロジェクトに追加したapp.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <!-- テスト用DBへの接続 --> <add name="Library.DataAccessLayer.Properties.Settings.LibraryConnectionString" connectionString="Data Source=localhost\SQLSERVER2005;Initial Catalog=Library;Persist Security Info=True;User ID=sa;Password=" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
#接続文字列にユーザー名とパスワードを入れるのは良くない。Windows認証でログインするべきだろう。ここではサンプルということであしからず。