DbConnectionStringBuilder クラス

定義

厳密に型指定された接続文字列ビルダーのための基底クラスを提供します。

public ref class DbConnectionStringBuilder : System::Collections::IDictionary
public ref class DbConnectionStringBuilder : System::Collections::IDictionary, System::ComponentModel::ICustomTypeDescriptor
public class DbConnectionStringBuilder : System.Collections.IDictionary
public class DbConnectionStringBuilder : System.Collections.IDictionary, System.ComponentModel.ICustomTypeDescriptor
type DbConnectionStringBuilder = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
type DbConnectionStringBuilder = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface ICustomTypeDescriptor
type DbConnectionStringBuilder = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ICustomTypeDescriptor
Public Class DbConnectionStringBuilder
Implements IDictionary
Public Class DbConnectionStringBuilder
Implements ICustomTypeDescriptor, IDictionary
継承
DbConnectionStringBuilder
派生
実装

次のコンソール アプリケーションは、Microsoft Jet データベース用とSQL Server データベース用の 2 つの接続文字列をビルドします。 いずれの場合も、コードはジェネリック DbConnectionStringBuilder クラスを使用して接続文字列を作成し、インスタンスの プロパティをDbConnectionStringBuilder厳密に型指定された接続クラスのコンストラクターに渡ConnectionStringします。 これは必須ではありません。コードでは、厳密に型指定された個々の接続文字列ビルダー インスタンスを作成することもできます。 この例では、既存の接続文字列も解析し、接続文字列の内容を操作するさまざまな方法を示します。

static void Main()
{
    DbConnectionStringBuilder builder =
        new DbConnectionStringBuilder();
    builder.ConnectionString = @"Data Source=c:\MyData\MyDb.mdb";
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
    builder.Add("Jet OLEDB:Database Password", "*******");
    builder.Add("Jet OLEDB:System Database",
        @"c:\MyData\Workgroup.mdb");
    // Set up row-level locking.
    builder.Add("Jet OLEDB:Database Locking Mode", 1);

    // The DbConnectionStringBuilder class
    // is database agnostic, so it's possible to
    // build any type of connection string using
    // this class.

    // The ConnectionString property may have been
    // formatted by the DbConnectionStringBuilder class.
    OleDbConnection oledbConnect = new
        OleDbConnection(builder.ConnectionString);
    Console.WriteLine(oledbConnect.ConnectionString);

    // Use the same DbConnectionStringBuilder to create
    // a SqlConnection object.
    builder.Clear();
    builder.Add("integrated security", true);
    builder.Add("Initial Catalog", "AdventureWorks");
    builder.Add("Data Source", "(local)");

    SqlConnection sqlConnect = new
        SqlConnection(builder.ConnectionString);
    Console.WriteLine(sqlConnect.ConnectionString);

    // Pass the DbConnectionStringBuilder an existing
    // connection string, and you can retrieve and
    // modify any of the elements.
    builder.ConnectionString = "server=(local);user id=*******;" +
        "password=*******;initial catalog=AdventureWorks";
    builder["Server"] = ".";
    builder.Remove("User ID");

    // Note that calling Remove on a nonexistent item doesn't
    // throw an exception.
    builder.Remove("BadItem");

    // Setting the indexer adds the value if
    // necessary.
    builder["Integrated Security"] = true;
    builder.Remove("password");
    builder["User ID"] = "Hello";
    Console.WriteLine(builder.ConnectionString);

    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();
}
Sub Main()
    Dim builder As New DbConnectionStringBuilder()
    builder.ConnectionString = "Data Source=c:\MyData\MyDb.mdb"
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
    builder.Add("Jet OLEDB:Database Password", "*******")
    builder.Add("Jet OLEDB:System Database", _
        "c:\MyData\Workgroup.mdb")

    ' Set up row-level locking.
    builder.Add("Jet OLEDB:Database Locking Mode", 1)

    ' Note that the DbConnectionStringBuilder class 
    ' is database agnostic, and it's possible to 
    ' build any type of connection string using 
    ' this class.
    ' Notice that the ConnectionString property may have been 
    ' formatted by the DbConnectionStringBuilder class.

    Dim oledbConnect As New _
        OleDbConnection(builder.ConnectionString)
    Console.WriteLine(oledbConnect.ConnectionString)

    ' Use the same DbConnectionStringBuilder to create 
    ' a SqlConnection object.
    builder.Clear()
    builder.Add("integrated security", True)
    builder.Add("Initial Catalog", "AdventureWorks")
    builder.Add("Data Source", "(local)")

    Dim sqlConnect As New SqlConnection(builder.ConnectionString)
    Console.WriteLine(sqlConnect.ConnectionString)

    ' Pass the DbConnectionStringBuilder an existing 
    ' connection string, and you can retrieve and
    ' modify any of the elements.
    builder.ConnectionString = _
        "server=(local);user id=*******;" & _
        "password=*******;initial catalog=AdventureWorks"
    builder.Item("Server") = "."
    builder.Remove("User ID")

    ' Note that calling Remove on a nonexistent item doesn't
    ' throw an exception.
    builder.Remove("BadItem")

    ' The Item property is the default for the class, 
    ' and setting the Item property adds the value if 
    ' necessary.
    builder("Integrated Security") = True
    builder.Remove("password")
    builder.Item("User ID") = "Hello"
    Console.WriteLine(builder.ConnectionString)

    Console.WriteLine("Press Enter to finish.")
    Console.ReadLine()
End Sub

注釈

クラスは DbConnectionStringBuilder 、厳密に型指定された接続文字列ビルダー (SqlConnectionStringBuilder、など OleDbConnectionStringBuilder) の派生元となる基本クラスを提供します。 接続文字列ビルダーを使用すると、開発者はプログラムによって構文的に正しい接続文字列を作成し、既存の接続文字列を解析して再構築できます。

DbConnectionStringBuilder 、データベースに依存しない方法で定義されています。 名前空間が追加されているため、開発者は任意の System.Data.Common データベースに対して動作する接続文字列を構築するためにプログラムできる基本クラスを必要とします。 したがって、 クラスを使用すると、ユーザーは DbConnectionStringBuilder 任意のキーと値のペアを割り当て、結果の接続文字列を厳密に型指定されたプロバイダーに渡すことができます。 .NET Frameworkの一部として含まれるすべてのデータ プロバイダーは、 からDbConnectionStringBuilderOdbcConnectionStringBuilderSqlConnectionStringBuilderOracleConnectionStringBuilder継承する厳密に型指定されたクラスをOleDbConnectionStringBuilder提供します。

開発者は、任意のプロバイダーの接続文字列をビルド、割り当て、編集できます。 特定のキーと値のペアをサポートするプロバイダーの場合、接続文字列ビルダーは、既知のペアに対応する厳密に型指定されたプロパティを提供します。 不明な値をサポートする機能を必要とするプロバイダーをサポートするために、開発者は任意のキーと値のペアを指定することもできます。

DbConnectionStringBuilder クラスは、ICustomTypeDescriptor インターフェイスを実装します。 つまり、クラスはデザイン時に Visual Studio デザイナーと連携します。 開発者がデザイナーを使用して厳密に型指定された DataSet と厳密に型指定された接続を Visual Studio 内に構築すると、厳密に型指定された接続文字列ビルダー クラスには、その型に関連付けられているプロパティが表示され、既知のキーの共通値をマップできるコンバーターも用意されます。

アプリケーションの一部として接続文字列を作成する必要がある開発者は、 クラスまたはその厳密に型指定された派生クラスの 1 つを使用 DbConnectionStringBuilder して、接続文字列をビルドおよび変更できます。 クラスを DbConnectionStringBuilder 使用すると、アプリケーション構成ファイルに格納されている接続文字列を簡単に管理できます。

開発者は、厳密に型指定された接続文字列ビルダー クラスを使用して接続文字列を作成することも、 クラスを DbConnectionStringBuilder 使用することもできます。 では DbConnectionStringBuilder 、有効なキーと値のペアのチェックは実行されません。 したがって、このクラスを使用して無効な接続文字列を作成することは可能です。 ではSqlConnectionStringBuilder、SQL Serverでサポートされているキーと値のペアのみがサポートされます。無効なペアを追加しようとすると、例外がスローされます。

メソッドとItem[]プロパティ ハンドルの両方がAdd、悪意のあるエントリの挿入を試みます。 たとえば、次のコードは、入れ子になったキーと値のペアを正しくエスケープします。

Dim builder As New System.Data.Common.DbConnectionStringBuilder
builder("Data Source") = "(local)"
builder("integrated sSecurity") = True
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";

結果は、無効な値が安全に処理される、次の接続文字列になります。

data source=(local);integrated security=True;
initial catalog="AdventureWorks;NewValue=Bad"

コンストラクター

DbConnectionStringBuilder()

DbConnectionStringBuilder クラスの新しいインスタンスを初期化します。

DbConnectionStringBuilder(Boolean)

DbConnectionStringBuilder クラスの新しいインスタンスを初期化します。必要に応じて、値を引用符で囲むことについての ODBC の規則が使用されます。

プロパティ

BrowsableConnectionString

ConnectionString プロパティを Visual Studio デザイナーに表示するかどうかを示す値を取得または設定します。

ConnectionString

DbConnectionStringBuilder に関連付けられた接続文字列を取得または設定します。

Count

ConnectionString プロパティ内に含まれる現在のキー数を取得します。

IsFixedSize

DbConnectionStringBuilder が固定サイズかどうかを示す値を取得します。

IsReadOnly

DbConnectionStringBuilder が読み取り専用かどうかを示す値を取得します。

Item[String]

指定されたキーに関連付けられている値を取得または設定します。

Keys

ICollection 内のキーが格納されている DbConnectionStringBuilder を取得します。

Values

ICollection 内の値を格納している DbConnectionStringBuilder を取得します。

メソッド

Add(String, Object)

指定したキーおよび値を持つエントリを DbConnectionStringBuilder に追加します。

AppendKeyValuePair(StringBuilder, String, String)

既存の StringBuilder オブジェクトにキーと値を追加する、効率的かつ安全な方法を提供します。

AppendKeyValuePair(StringBuilder, String, String, Boolean)

既存の StringBuilder オブジェクトにキーと値を追加する、効率的かつ安全な方法を提供します。

Clear()

DbConnectionStringBuilder インスタンスの内容を消去します。

ClearPropertyDescriptors()

関連する DbConnectionStringBuilder 上の PropertyDescriptor オブジェクトのコレクションをクリアします。

ContainsKey(String)

DbConnectionStringBuilder に特定のキーが格納されているかどうかを判断します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
EquivalentTo(DbConnectionStringBuilder)

この DbConnectionStringBuilder オブジェクトの接続情報を、提供されたオブジェクトの接続情報を比較します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetProperties(Hashtable)

指定された Hashtable に、この DbConnectionStringBuilder のすべてのプロパティに関する情報を格納します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(String)

指定されたキーを持つエントリを DbConnectionStringBuilder インスタンスから削除します。

ShouldSerialize(String)

指定されたキーが、この DbConnectionStringBuilder インスタンスに存在するかどうかを示します。

ToString()

DbConnectionStringBuilder に関連付けられた接続文字列を返します。

TryGetValue(String, Object)

提供されたキーに対応する値をこの DbConnectionStringBuilder から取得します。

明示的なインターフェイスの実装

ICollection.CopyTo(Array, Int32)

ICollection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。

ICollection.IsSynchronized

ICollection へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

ICollection.SyncRoot

ICollection へのアクセスを同期するために使用できるオブジェクトを取得します。

ICustomTypeDescriptor.GetAttributes()

コンポーネントのこのインスタンスのカスタム属性のコレクションを返します。

ICustomTypeDescriptor.GetClassName()

コンポーネントのこのインスタンスのクラス名を返します。

ICustomTypeDescriptor.GetComponentName()

コンポーネントのこのインスタンスの名前を返します。

ICustomTypeDescriptor.GetConverter()

コンポーネントのこのインスタンスの型コンバーターを返します。

ICustomTypeDescriptor.GetDefaultEvent()

コンポーネントのこのインスタンスの既定のイベントを返します。

ICustomTypeDescriptor.GetDefaultProperty()

コンポーネントのこのインスタンスの既定のプロパティを返します。

ICustomTypeDescriptor.GetEditor(Type)

コンポーネントのこのインスタンスに対して指定されている型のエディターを返します。

ICustomTypeDescriptor.GetEvents()

コンポーネントのこのインスタンスのイベントを返します。

ICustomTypeDescriptor.GetEvents(Attribute[])

フィルターとして指定された属性配列を使用して、コンポーネントのこのインスタンスのイベントを返します。

ICustomTypeDescriptor.GetProperties()

コンポーネントのこのインスタンスのプロパティを返します。

ICustomTypeDescriptor.GetProperties(Attribute[])

属性配列をフィルターとして使用して、コンポーネントのこのインスタンスのプロパティを返します。

ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)

指定したプロパティ記述子によって記述されたプロパティを含むオブジェクトを返します。

IDictionary.Add(Object, Object)

指定したキーおよび値を持つ要素を IDictionary オブジェクトに追加します。

IDictionary.Contains(Object)

指定したキーを持つ要素が IDictionary オブジェクトに格納されているかどうかを確認します。

IDictionary.GetEnumerator()

IDictionary オブジェクトの IDictionaryEnumerator オブジェクトを返します。

IDictionary.IsFixedSize

IDictionary オブジェクトが固定サイズかどうかを示す値を取得します。

IDictionary.IsReadOnly

IDictionary が読み取り専用かどうかを示す値を取得します。

IDictionary.Item[Object]

指定したキーを持つ要素を取得または設定します。

IDictionary.Remove(Object)

指定したキーを持つ要素を IDictionary オブジェクトから削除します。

IEnumerable.GetEnumerator()

コレクションを反復処理する列挙子を返します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください