connectionStringsの書式に関する情報
通常はweb.configまたはapp.configの設定ファイル内で、connectionStrings構成セクションを使用してデータ接続文字列を定義することができます。 connectionStrings構成セクションには、1つ以上のconnectionString要素が含まれており、それぞれが1つのデータ接続を表します。
以下はconnectionStringsの書き方の例です。
- 文字列を明示的に連結すること。
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" providerName="System.Data.SqlClient" />
</connectionStrings>
上記の例では、name属性は接続文字列の名前を指し、connectionString属性は実際の接続文字列を指し、providerName属性は使用されるデータプロバイダーを指定します(ここではSQL Serverのプロバイダーが使用されています)。
- 暗号化された接続文字列を使用します。
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>Base64-encoded encrypted connection string</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
以上の例では、configProtectionProvider属性は使用する暗号化プロバイダを指定します。暗号化された接続文字列は、指定された暗号化プロバイダで暗号化されたものであるべきです。
- 外部の設定ファイルで指定した接続文字列を使用します。
<connectionStrings configSource="connections.config" />
指定したconfigSource属性は、外部の設定ファイルを指定し、実際の接続文字列が含まれています。接続文字列を別のファイルに保存することで、接続文字列を簡単に変更および管理できるため、メインの設定ファイルを変更する必要はありません。
connections.configファイルには、接続文字列が以下のように定義されています。
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" providerName="System.Data.SqlClient" />
</connectionStrings>
connections.configファイルは、メインの設定ファイルと同じディレクトリに置かれるか、位置を相対または絶対パスで指定する必要があります。
上記は”connectionStrings”に関する一般的な書き方についての情報です。具体的な要件に応じてデータ接続を構成する方法を選択することができます。