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 データベースの接続文字列を作成します。 この例では、既存の接続文字列も解析し、接続文字列の内容を操作するさまざまな方法を示します。

注意

この例には、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 = _
        "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()

    ' 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 デザイナーと連携します。 開発者がデザイナーを使用して Visual Studio .NET 内で厳密に型指定された DataSet と厳密に型指定された接続を構築する場合、厳密に型指定された 接続文字列 ビルダー クラスには、その型に関連付けられているプロパティが表示され、既知のキーの共通値をマップできるコンバーターも含まれます。

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

Key プロパティ 既定値
ファイル名 FileName ""
プロバイダー Provider ""
Data Source DataSource ""
Persist Security Info PersistSecurityInfo ×
OLE DB Services (OLE DB サービス) 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()

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

(継承元 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()

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

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

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

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

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

(継承元 DbConnectionStringBuilder)
IEnumerable.GetEnumerator()

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

(継承元 DbConnectionStringBuilder)

拡張メソッド

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

こちらもご覧ください