OleDbConnectionStringBuilder クラス

定義

OleDbConnection クラスで使用される接続文字列の内容を簡単に作成および管理できます。

public ref class OleDbConnectionStringBuilder sealed : System::Data::Common::DbConnectionStringBuilder
[System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder+OleDbConnectionStringBuilderConverter))]
public sealed class OleDbConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
[<System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder+OleDbConnectionStringBuilderConverter))>]
type OleDbConnectionStringBuilder = class
    inherit DbConnectionStringBuilder
Public NotInheritable Class OleDbConnectionStringBuilder
Inherits DbConnectionStringBuilder
継承
OleDbConnectionStringBuilder
属性

次のコンソール アプリケーションは、複数の OLE DB データベースの接続文字列をビルドします。 最初に、Microsoft Access データベースの接続文字列を作成してから、IBM DB2 データベースの接続文字列を作成します。 この例では、既存の接続文字列も解析し、接続文字列の内容を操作するさまざまな方法を示します。

Note

この例には、接続文字列で OleDbConnectionStringBuilder がどのように機能するかを示すパスワードが含まれています。 アプリケーションでは、Windows 認証を使用することをお勧めします。 パスワードを使用する必要がある場合は、ハードコーディングされたパスワードをアプリケーションに含めないでください。

using System.Data.OleDb;

class Program
{
    static void Main(string[] args)
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.ConnectionString = @"Data Source=C:\Sample.mdb";

        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
        builder.Add("Jet OLEDB:Database Password", "MyPassword!");
        builder.Add("Jet OLEDB:System Database", @"C:\Workgroup.mdb");

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

        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Clear current values and reset known keys to their
        // default values.
        builder.Clear();

        // Pass the OleDbConnectionStringBuilder an existing
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString =
            "Provider=DB2OLEDB;Network Transport Library=TCPIP;" +
            "Network Address=192.168.0.12;Initial Catalog=DbAdventures;" +
            "Package Collection=SamplePackage;Default Schema=SampleSchema;";

        Console.WriteLine("Network Address = " + builder["Network Address"].ToString());
        Console.WriteLine();

        // Modify existing items.
        builder["Package Collection"] = "NewPackage";
        builder["Default Schema"] = "NewSchema";

        // Call the Remove method to remove items from
        // the collection of key/value pairs.
        builder.Remove("User ID");

        // Note that calling Remove on a nonexistent item does not
        // throw an exception.
        builder.Remove("BadItem");
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Setting the indexer adds the value, if
        // necessary.
        builder["User ID"] = "SampleUser";
        builder["Password"] = "SamplePassword";
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}
Imports System.Data.OleDb
Imports System.Collections

Module Module1
  Sub Main()
    Dim builder As New OleDbConnectionStringBuilder()
    builder.ConnectionString = "Data Source=C:\Sample.mdb"

    ' Call the Add method to explicitly add key/value
    ' pairs to the internal collection.
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
    builder.Add("Jet OLEDB:Database Password", "MyPassword!")
    builder.Add("Jet OLEDB:System Database", "C:\Workgroup.mdb")

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

    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()

    ' Clear current values and reset known keys to their
    ' default values.
    builder.Clear()

    ' Pass the OleDbConnectionStringBuilder an existing
    ' connection string, and you can retrieve and
    ' modify any of the elements.
    builder.ConnectionString = "..."

    Console.WriteLine("Network Address = " & builder("Network Address").ToString())
    Console.WriteLine()

    ' Modify existing items.
    builder("Package Collection") = "NewPackage"
    builder("Default Schema") = "NewSchema"

    ' Call the Remove method to remove items from
    ' the collection of key/value pairs.
    builder.Remove("User ID")

    ' Note that calling Remove on a nonexistent item does not
    ' throw an exception.
    builder.Remove("BadItem")
    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()

    ' The Item property is the default for the class,
    ' and setting the Item property adds the value, if
    ' necessary.
    builder("User ID") = "SampleUser"
    builder("Password") = "SamplePassword"
    Console.WriteLine(builder.ConnectionString)

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

注釈

接続文字列ビルダーを使用すると、開発者はプログラムで構文的に正しい接続文字列を作成し、クラスのプロパティとメソッドを使用して既存の接続文字列を解析して再構築できます。 接続文字列 ビルダーは、OLE DB 接続で許可されている既知のキーと値のペアに対応する厳密に型指定されたプロパティを提供し、開発者は他の接続文字列値に任意のキーと値のペアを追加できます。 OleDbConnectionStringBuilder クラスは ICustomTypeDescriptor インターフェイスを実装しています。 つまり、クラスはデザイン時にVisual Studio .NETデザイナーと連携します。 開発者がデザイナーを使用して厳密に型指定された DataSet と厳密に型指定された接続をVisual Studio .NET内に構築する場合、厳密に型指定された接続文字列 ビルダー クラスには、その型に関連付けられているプロパティが表示され、既知のキーの共通値をマップできるコンバーターも含まれます。

アプリケーションの一部として接続文字列を作成する必要がある開発者は、 OleDbConnectionStringBuilder クラスを使用して接続文字列をビルドおよび変更できます。 また、このクラスを使用すると、アプリケーション構成ファイルに格納されている接続文字列を簡単に管理できます。 OleDbConnectionStringBuilderは、既知のキーと値のペアの限られたセットについてのみチェックを実行します。 そのため、このクラスを使用して無効な接続文字列を作成できます。 次の表に、 OleDbConnectionStringBuilder クラス内の既知のキーとそれに対応するプロパティと、その既定値を示します。 これらの特定の値に加えて、開発者は、 OleDbConnectionStringBuilder インスタンス内に含まれる任意のキーと値のペアをコレクションに追加できます。

財産 既定値
ファイル名 FileName ""
Provider Provider ""
データ ソース DataSource ""
セキュリティ情報を保持する PersistSecurityInfo いいえ
OLE DB Services OleDbServices -13

Item[] プロパティは、悪意のあるエントリの挿入試行を処理します。 たとえば、次のコードでは、既定の Item[] プロパティ (C# のインデクサー) を使用すると、入れ子になったキーと値のペアが正しくエスケープされます。

Dim builder As _
    New System.Data.OleDb.OleDbConnectionStringBuilder
builder("Provider") = "Microsoft.Jet.OLEDB.4.0"
builder("Data Source") = "C:\Sample.mdb"
builder("User Id") = "Admin;NewValue=Bad"
System.Data.OleDb.OleDbConnectionStringBuilder builder =
    new System.Data.OleDb.OleDbConnectionStringBuilder();
builder["Provider"] = "Microsoft.Jet.OLEDB.4.0";
builder["Data Source"] = "C:\\Sample.mdb";
builder["User Id"] = "Admin;NewValue=Bad";

結果は、無効な値を安全な方法で処理する次の接続文字列です。

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Sample.mdb;User ID="Admin;NewValue=Bad"

コンストラクター

名前 説明
OleDbConnectionStringBuilder()

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

OleDbConnectionStringBuilder(String)

OleDbConnectionStringBuilder クラスの新しいインスタンスを初期化します。 指定された接続文字列は、インスタンスの内部接続情報のデータを提供します。

プロパティ

名前 説明
BrowsableConnectionString

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

(継承元 DbConnectionStringBuilder)
ConnectionString

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

(継承元 DbConnectionStringBuilder)
Count

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

(継承元 DbConnectionStringBuilder)
DataSource

接続するデータ ソースの名前を取得または設定します。

FileName

データ ソースに接続するためのユニバーサル データ リンク (UDL) ファイルの名前を取得または設定します。

IsFixedSize

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

(継承元 DbConnectionStringBuilder)
IsReadOnly

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

(継承元 DbConnectionStringBuilder)
Item[String]

指定したキーに関連付けられている値を取得または設定します。 C# では、このプロパティはインデクサーです。

Keys

ICollection内のキーを含むOleDbConnectionStringBuilderを取得します。

OleDbServices

接続文字列内の OLE DB Services キーに渡される値を取得または設定します。

PersistSecurityInfo

接続が開いている場合、または開いている状態になった場合に、パスワードなどのセキュリティに依存する情報が接続の一部として返されるかどうかを示すブール値を取得または設定します。

Provider

内部接続文字列に関連付けられているデータ プロバイダーの名前を含む文字列を取得または設定します。

Values

ICollection内の値を含むDbConnectionStringBuilderを取得します。

(継承元 DbConnectionStringBuilder)

メソッド

名前 説明
Add(String, Object)

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

(継承元 DbConnectionStringBuilder)
Clear()

OleDbConnectionStringBuilder インスタンスの内容をクリアします。

ClearPropertyDescriptors()

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

(継承元 DbConnectionStringBuilder)
ContainsKey(String)

OleDbConnectionStringBuilder に特定のキーが含まれているかどうかを判断します。

Equals(Object)

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

(継承元 Object)
EquivalentTo(DbConnectionStringBuilder)

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

(継承元 DbConnectionStringBuilder)
GetHashCode()

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

(継承元 Object)
GetProperties(Hashtable)

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

(継承元 DbConnectionStringBuilder)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
Remove(String)

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

ShouldSerialize(String)

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

(継承元 DbConnectionStringBuilder)
ToString()

このDbConnectionStringBuilderに関連付けられている接続文字列を返します。

(継承元 DbConnectionStringBuilder)
TryGetValue(String, Object)

OleDbConnectionStringBuilder インスタンスから、指定されたキーに対応する値を取得します。

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

名前 説明
ICollection.CopyTo(Array, Int32)

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

(継承元 DbConnectionStringBuilder)
ICollection.IsSynchronized

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

(継承元 DbConnectionStringBuilder)
ICollection.SyncRoot

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetAttributes()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetClassName()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetComponentName()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetConverter()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetDefaultEvent()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetDefaultProperty()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEditor(Type)

コンポーネントのこのインスタンスの指定した型のエディターを返します。

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEvents()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEvents(Attribute[])

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetProperties()

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetProperties(Attribute[])

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

(継承元 DbConnectionStringBuilder)
ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)

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

(継承元 DbConnectionStringBuilder)
IDictionary.Add(Object, Object)

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

(継承元 DbConnectionStringBuilder)
IDictionary.Contains(Object)

IDictionary オブジェクトに、指定したキーを持つ要素が含まれているかどうかを判断します。

(継承元 DbConnectionStringBuilder)
IDictionary.GetEnumerator()

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

(継承元 DbConnectionStringBuilder)
IDictionary.Item[Object]

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

(継承元 DbConnectionStringBuilder)
IDictionary.Remove(Object)

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

(継承元 DbConnectionStringBuilder)
IEnumerable.GetEnumerator()

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

(継承元 DbConnectionStringBuilder)

拡張メソッド

名前 説明
AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

適用対象

こちらもご覧ください