DbConnectionStringBuilder クラス
アセンブリ: System.Data (system.data.dll 内)

Public Class DbConnectionStringBuilder Implements IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor
public class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor
public ref class DbConnectionStringBuilder : IDictionary, ICollection, IEnumerable, ICustomTypeDescriptor

DbConnectionStringBuilder クラスにより、厳密に型指定された接続文字列ビルダ (SqlConnectionStringBuilder や OleDbConnectionStringBuilder など) の派生元となる基本クラスが得られます。この接続文字列ビルダを使用して、正確な構文の接続文字列をプログラムから作成したり、既存の接続文字列を解析、再構築したりできます。
DbConnectionStringBuilder はデータベースを選ばない方法で定義されていました。System.Data.Common 名前空間が追加されたため、任意のデータベースに対して有効な接続文字列を構築するためにプログラムで使用できる基本クラスが必要です。つまり、DbConnectionStringBuilder クラスによってユーザーは任意のキー/値ペアを割り当て、生成された接続文字列を厳密に型指定されたプロバイダに渡すことができます。.NET Framework の一部となっているすべてのデータ プロバイダが、DbConnectionStringBuilder から継承された厳密に型指定されたクラスを提供します。そのクラスとは、SqlConnectionStringBuilder、OracleConnectionStringBuilder、OdbcConnectionStringBuilder、OleDbConnectionStringBuilder です。
任意のプロバイダの接続文字列を構築、割り当て、編集できます。特定のキー/値ペアをサポートするプロバイダの場合、接続文字列ビルダによってそのペアに対応する厳密に型指定されたプロパティが得られます。未知の値をサポートする機能が要求されるプロバイダをサポートするために、任意のキー/値ペアを指定することもできます。
DbConnectionStringBuilder クラスは、ICustomTypeDescriptor インターフェイスを実装します。つまり、このクラスはデザイン時に Visual Studio デザイナと組み合わせて使用できます。デザイナを使用して、Visual Studio で厳密に型指定された DataSet と厳密に型指定された接続を構築すると、厳密に型指定された接続文字列ビルダ クラスでは、その型に関連付けられたプロパティが表示され、さらに既知のキーに共通の値を割り当てられるコンバータが得られます。
接続文字列をアプリケーション内で生成する必要がある場合、DbConnectionStringBuilder クラス、またはその厳密に型指定された派生クラスを使用して、接続文字列を構築したり修正したりできます。また、DbConnectionStringBuilder クラスを利用することで、アプリケーションの構成ファイルに格納される接続文字列の管理が容易になります。
厳密に型指定された接続文字列ビルダ クラス、または DbConnectionStringBuilder クラスを使用して、接続文字列を作成できます。DbConnectionStringBuilder では、キー/値ペアが有効であるかどうかのチェックは実行されません。したがって、このクラスを使用すると無効な接続文字列が作成される可能性があります。SqlConnectionStringBuilder では、SQL Server でサポートされるキー/値ペアのみがサポートされます。無効なペアを追加しようとすると、例外がスローされます。
悪意のあるエントリを挿入しようとする操作は、Add メソッドと Item プロパティによって処理されます。たとえば、入れ子になったキー/値ペアは次のコードによって適切にエスケープされます。
Dim builder As New System.Data.Common.DbConnectionStringBuilder builder("Data Source") = "(local)" builder("integrated sSecurity") = True builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"
[C#]

Microsoft Jet データベースと Microsoft SQL Server 2005 データベース用に、2 つの接続文字列を構築するコンソール アプリケーションを次に示します。どちらの接続文字列の場合も、このコードでは汎用の DbConnectionStringBuilder クラスを使用して接続文字列を作成し、DbConnectionStringBuilder インスタンスの ConnectionString プロパティを厳密に型指定された接続クラスのコンストラクタに渡しています。この動作は必須ではありません。コードでは、厳密に型指定された接続文字列ビルダのインスタンスを個別に作成することもできます。さらにこの例では、既存の接続文字列を解析し、接続文字列の内容に対して各種の操作を行う例が示されています。
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
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(); }

System.Data.Common.DbConnectionStringBuilder
System.Data.Odbc.OdbcConnectionStringBuilder
System.Data.OleDb.OleDbConnectionStringBuilder
System.Data.OracleClient.OracleConnectionStringBuilder
System.Data.SqlClient.SqlConnectionStringBuilder


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DbConnectionStringBuilder クラスのページへのリンク