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


接続文字列ビルダにより、クラスのプロパティとメソッドを使用して、構文的に正確な接続文字列をプログラムで作成したり、既存の接続文字列を解析および再構築したりできます。接続文字列ビルダは、ODBC 接続がサポートする既知のキー/値ペアに対応した、厳密に型指定されたプロパティを提供するほか、開発者が他の接続文字列値に対して任意のキー/値ペアを追加することもできます。
接続文字列をアプリケーション内で生成する必要がある場合、OdbcConnectionStringBuilder クラスを使用して、接続文字列を生成および修正できます。また、このクラスを利用することで、アプリケーションの構成ファイルに格納される接続文字列の管理が容易になります。OdbcConnectionStringBuilder によって実行されるチェックは、既知のキー/値ペアに限定されます。したがって、このクラスを使用した場合、無効な接続文字列が生成される場合もあります。既知のキーと、OdbcConnectionStringBuilder クラス内の対応するプロパティ、およびその既定値の一覧を次の表に示します。これらの特定の値以外にも、開発者は任意のキー/値ペアを、OdbcConnectionStringBuilder のインスタンス内に保持されるコレクションに追加できます。
Driver プロパティを設定する場合、ドライバ名を中かっこ ({}) で囲む必要はありません。OdbcConnectionStringBuilder のインスタンスにより、中かっこ ({}) が必要に応じて追加されます。 | |||
|
接続文字列中の値 (Driver 値を除く) に、セミコロン (;) が含まれていた場合、OdbcConnectionStringBuilder は、その値の両側に引用符を追加して接続文字列に埋め込みます。セミコロンが使用される頻度の高い Driver 値については、この問題を回避するため、OdbcConnectionStringBuilder クラスによって常に両側に中かっこが追加されます。ODBC の仕様では、セミコロンを含むドライバ値は中かっこで囲むという決まりがあります。このクラスでは、この処理が自動的に行われます。
Item プロパティは、安全ではないコードの挿入が試みられた場合も、適切に処理します。たとえば、次のコードでは、既定の Item プロパティ (C# ではインデクサ) を使用することで、入れ子になったキー/値ペアが適切にエスケープされています。
Dim builder As _ New System.Data.Odbc.OdbcConnectionStringBuilder ' Take advantage of the Driver property. builder.Driver = "SQL Server" builder("Server") = "MyServer;NewValue=Bad" Console.WriteLine(builder.ConnectionString)
[C#]
System.Data.Odbc.OdbcConnectionStringBuilder builder = new System.Data.Odbc.OdbcConnectionStringBuilder(); // Take advantage of the Driver property. builder.Driver = "SQL Server"; builder["Server"] = "MyServer;NewValue=Bad"; Console.WriteLine(builder.ConnectionString);
結果は次の接続文字列になり、無効な値は安全な方法で処理されます。
Driver={SQL Server};Server="MyServer;NewValue=Bad"

次のコンソール アプリケーションでは、いくつかの ODBC データベースで使用される接続文字列を作成します。最初に、Microsoft Access データベース用の接続文字列を作成します。次に、IBM DB2 データベース用の接続文字列を作成します。また、既存の接続文字列を解析し、接続文字列の内容に対して各種の操作を行う例が示されています。
![]() |
---|
この例には、OdbcConnectionStringBuilder と接続文字列がどのように連携するかを示すパスワードが含まれています。アプリケーションでは、Windows 認証の使用をお勧めします。パスワードを使用する必要がある場合は、ハードコーディングされたパスワードをアプリケーションに組み込まないでください。 |
Imports System.Data.Odbc Module Module1 Sub Main() Dim builder As New OdbcConnectionStringBuilder() builder.Driver = "Microsoft Access Driver (*.mdb)" ' Call the Add method to explicitly add key/value ' pairs to the internal collection. builder.Add("Dbq", "C:\info.mdb") builder.Add("Uid", "Admin") builder.Add("Pwd", "pass!word1") Console.WriteLine(builder.ConnectionString) Console.WriteLine() ' Clear current values and reset known keys to their ' default values. builder.Clear() ' Pass the OdbcConnectionStringBuilder an existing ' connection string, and you can retrieve and ' modify any of the elements. builder.ConnectionString = _ "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" & _ "hostname=SampleServerName;port=SamplePortNum;" & _ "protocol=TCPIP;uid=Admin;pwd=pass!word1" Console.WriteLine("protocol = " & builder("protocol").ToString()) Console.WriteLine() ' Modify existing items: builder("uid") = "NewUser" builder("pwd") = "Pass@word2" ' Call the Remove method to remove items from ' the collection of key/value pairs. builder.Remove("port") ' 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("NewKey") = "newValue" Console.WriteLine(builder.ConnectionString) Console.WriteLine("Press Enter to finish.") Console.ReadLine() End Sub End Module
using System.Data.Odbc; class Program { static void Main() { OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(); builder.Driver = "Microsoft Access Driver (*.mdb)"; // Call the Add method to explicitly add key/value // pairs to the internal collection. builder.Add("Dbq", "C:\\info.mdb"); builder.Add("Uid", "Admin"); builder.Add("Pwd", "pass!word1"); Console.WriteLine(builder.ConnectionString); Console.WriteLine(); // Clear current values and reset known keys to their // default values. builder.Clear(); // Pass the OdbcConnectionStringBuilder an existing // connection string, and you can retrieve and // modify any of the elements. builder.ConnectionString = "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" + "hostname=SampleServerName;port=SamplePortNum;" + "protocol=TCPIP;uid=Admin;pwd=pass!word1"; Console.WriteLine("protocol = " + builder["protocol"].ToString()); Console.WriteLine(); // Modify existing items. builder["uid"] = "NewUser"; builder["pwd"] = "Pass@word2"; // Call the Remove method to remove items from // the collection of key/value pairs. builder.Remove("port"); // 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 associated value, if // necessary. builder["NewKey"] = "newValue"; Console.WriteLine(builder.ConnectionString); Console.WriteLine("Press Enter to finish."); Console.ReadLine(); } }

System.Data.Common.DbConnectionStringBuilder
System.Data.Odbc.OdbcConnectionStringBuilder


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


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