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


接続文字列ビルダにより、クラスのプロパティとメソッドを使用して、構文的に正確な接続文字列をプログラムで作成したり、既存の接続文字列を解析および再構築したりできます。接続文字列ビルダは、OLE DB 接続がサポートする既知のキー/値ペアに対応した、厳密に型指定されたプロパティを提供するほか、開発者が他の接続文字列値に対して任意のキー/値ペアを追加することもできます。OleDbConnectionStringBuilder クラスは、ICustomTypeDescriptor インターフェイスを実装します。つまり、クラスと Visual Studio .NET デザイナがデザイン時に連動するということです。Visual Studio .NET のデザイナを使用して厳密に型指定された DataSet および厳密に型指定された接続を作成することにより、厳密に型指定された接続文字列ビルダ クラスによって、その型に関連付けられたプロパティが表示されるほか、既知のキーに共通の値をマップするためのコンバータを使用できるようになります。
接続文字列をアプリケーション内で生成する必要がある場合、OleDbConnectionStringBuilder クラスを使用して、接続文字列を生成および修正できます。また、このクラスを利用することで、アプリケーションの構成ファイルに格納される接続文字列の管理が容易になります。OleDbConnectionStringBuilder によって実行されるチェックは、既知のキー/値ペアに限定されます。したがって、このクラスを使用した場合、無効な接続文字列が生成される場合もあります。既知のキーと、OleDbConnectionStringBuilder クラス内の対応するプロパティ、およびその既定値の一覧を次の表に示します。これらの特定の値以外にも、開発者は任意のキー/値ペアを、OleDbConnectionStringBuilder のインスタンス内に保持されるコレクションに追加できます。
"" | ||
"" | ||
DataSource | "" | |
PersistSecurityInfo | ||
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"

次のコンソール アプリケーションでは、いくつかの OLE DB データベースで使用される接続文字列を作成します。最初に、Microsoft Access データベース用の接続文字列を作成した後、IBM DB2 データベース用の接続文字列を作成します。また、既存の接続文字列を解析し、接続文字列の内容に対して各種の操作を行う例が示されています。
![]() |
---|
この例には、OleDbConnectionStringBuilder と接続文字列がどのように連携するかを示すパスワードが含まれています。アプリケーションでは、Windows 認証の使用をお勧めします。パスワードを使用する必要がある場合は、ハードコーディングされたパスワードをアプリケーションに組み込まないでください。 |
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
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(); } }

System.Data.Common.DbConnectionStringBuilder
System.Data.OleDb.OleDbConnectionStringBuilder


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


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