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


接続文字列ビルダにより、クラスのプロパティとメソッドを使用して、構文的に正確な接続文字列をプログラムで作成したり、既存の接続文字列を解析および再構築したりできます。接続文字列ビルダは、SQL Server がサポートする既知のキー/値ペアに対応した、厳密に型指定されたプロパティを提供します。接続文字列をアプリケーション内で生成する必要がある場合、SqlConnectionStringBuilder クラスを使用して、接続文字列を生成および修正できます。また、このクラスを利用することで、アプリケーションの構成ファイルに格納される接続文字列の管理が容易になります。
SqlConnectionStringBuilder では、キー/値ペアが有効であるかどうかのチェックが実行されます。したがって、このクラスを使った場合、無効な接続文字列が生成されることはありません。無効なペアを追加しようとすると例外がスローされます。このクラスは、一連のシノニムに対する固定のコレクションを保持しており、シノニムから対応する既知のキー名への変換を実行します。
たとえば、Item プロパティで、必要なキーに対応するシノニムを含んだ文字列を指定することによって値を取得できます。たとえば、Item プロパティや Remove メソッドなど、キー名を含んだ文字列を引数として受け取るメンバを使用する場合、"Network Address" や "addr" など、接続文字列内のキーに対してサポートされたシノニムを指定できます。サポートされるすべてのシノニムの一覧については、ConnectionString プロパティのトピックを参照してください。
Item プロパティは、安全ではないエントリの挿入が試みられた場合も、適切に処理します。たとえば、次のコードでは、既定の Item プロパティ (C# ではインデクサ) を使用することで、入れ子になったキー/値ペアが適切にエスケープされています。
Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder builder("Data Source") = "(local)" builder("Integrated Security") = True builder("Initial Catalog") = "AdventureWorks;NewValue=Bad" Console.WriteLine(builder.ConnectionString)

次のコンソール アプリケーションでは、SQL Server 2005 のデータベースで使用される接続文字列を作成します。このコードでは、SqlConnectionStringBuilder クラスを使用して接続文字列を作成し、SqlConnectionStringBuilder インスタンスの ConnectionString プロパティを、接続クラスのコンストラクタに渡しています。また、既存の接続文字列を解析し、接続文字列の内容に対して各種の操作を行う例が示されています。
![]() |
---|
この例には、SqlConnectionStringBuilder と接続文字列がどのように連携するかを示すパスワードが含まれています。アプリケーションでは、Windows 認証の使用をお勧めします。パスワードを使用する必要がある場合は、ハードコーディングされたパスワードをアプリケーションに組み込まないでください。 |
Imports System.Data.SqlClient Module Module1 Sub Main() ' Create a new SqlConnectionStringBuilder and ' initialize it with a few name/value pairs: Dim builder As New SqlConnectionStringBuilder(GetConnectionString()) ' The input connection string used the ' Server key, but the new connection string uses ' the well-known Data Source key instead. Console.WriteLine(builder.ConnectionString) ' Pass the SqlConnectionStringBuilder an existing ' connection string, and you can retrieve and ' modify any of the elements. builder.ConnectionString = _ "server=(local);user id=ab;" & _ "password=a!Pass113;initial catalog=AdventureWorks" ' Now that the connection string has been parsed, ' you can work with individual items. Console.WriteLine(builder.Password) builder.Password = "new@1Password" builder.AsynchronousProcessing = True ' You can refer to connection keys using strings, ' as well. When you use this technique (the default ' Item property in Visual Basic, or the indexer in C#) ' you can specify any synonym for the connection string key ' name. builder("Server") = "." builder("Connect Timeout") = 1000 ' The Item property is the default for the class, ' and setting the Item property adds the value to the ' dictionary, if necessary. builder.Item("Trusted_Connection") = True Console.WriteLine(builder.ConnectionString) Console.WriteLine("Press Enter to finish.") Console.ReadLine() End Sub Private Function GetConnectionString() As String ' To avoid storing the connection string in your code, ' you can retrieve it from a configuration file. Return "Server=(local);Integrated Security=SSPI;" & _ "Initial Catalog=AdventureWorks" End Function End Module
using System.Data; using System.Data.SqlClient; class Program { static void Main() { // Create a new SqlConnectionStringBuilder and // initialize it with a few name/value pairs. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(GetConnectionString()); // The input connection string used the // Server key, but the new connection string uses // the well-known Data Source key instead. Console.WriteLine(builder.ConnectionString); // Pass the SqlConnectionStringBuilder an existing // connection string, and you can retrieve and // modify any of the elements. builder.ConnectionString = "server=(local);user id=ab;" + "password= a!Pass113;initial catalog=AdventureWorks"; // Now that the connection string has been parsed, // you can work with individual items. Console.WriteLine(builder.Password); builder.Password = "new@1Password"; builder.AsynchronousProcessing = true; // You can refer to connection keys using strings, // as well. When you use this technique (the default // Item property in Visual Basic, or the indexer in C#), // you can specify any synonym for the connection string key // name. builder["Server"] = "."; builder["Connect Timeout"] = 1000; builder["Trusted_Connection"] = true; Console.WriteLine(builder.ConnectionString); Console.WriteLine("Press Enter to finish."); Console.ReadLine(); } private static string GetConnectionString() { // To avoid storing the connection string in your code, // you can retrieve it from a configuration file. return "Server=(local);Integrated Security=SSPI;" + "Initial Catalog=AdventureWorks"; } }

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


SqlConnectionStringBuilder コンストラクタ ()
アセンブリ: System.Data (system.data.dll 内)


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


SqlConnectionStringBuilder コンストラクタ (String)
アセンブリ: System.Data (system.data.dll 内)



SqlConnectionStringBuilder クラスは、キー/値ペアの固定のコレクションを内部的に保持しています。このコンストラクタに対して接続文字列の値を部分的に指定した場合でも、オブジェクトからは常に各キー/値ペアの既定値が提供されます。このオブジェクトの ConnectionString プロパティで取得される文字列には、項目の既定値以外のキー/値ペアだけが格納されます。

次の例では、SQL Server の単純な接続文字列を、SqlConnectionStringBuilder オブジェクトのコンストラクタに指定し、そのオブジェクトに含まれたすべてのキー/値ペアを反復処理しています。このコレクションには、各項目の既定値が含まれる点に注意してください。また、SqlConnectionStringBuilder クラスは、常に一貫したキー名となるよう、既知のキーに対するシノニムが変換されます。
![]() |
---|
この例には、SqlConnectionStringBuilder と接続文字列がどのように連携するかを示すパスワードが含まれています。アプリケーションでは、Windows 認証の使用をお勧めします。パスワードを使用する必要がある場合は、ハードコーディングされたパスワードをアプリケーションに組み込まないでください。 |
Imports System.Data.SqlClient Module Module1 Sub Main() Try Dim connectString As String = _ "Server=(local);Database=AdventureWorks;UID=ab;Pwd=a!Pass@@" Console.WriteLine("Original: " & connectString) Dim builder As New SqlConnectionStringBuilder(connectString) Console.WriteLine("Modified: " & builder.ConnectionString) For Each key As String In builder.Keys Console.WriteLine(key & "=" & builder.Item(key).ToString) Next Console.WriteLine("Press any key to finish.") Console.ReadLine() Catch ex As System.Collections.Generic.KeyNotFoundException Console.WriteLine("KeyNotFoundException: " & ex.Message) Catch ex As System.FormatException Console.WriteLine("Format exception: " & ex.Message) End Try End Sub End Module
using System.Data.SqlClient; class Program { static void Main() { try { string connectString = "Server=(local);Database=AdventureWorks;UID=ab;Pwd= a!Pass@@"; Console.WriteLine("Original: " + connectString); SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString); Console.WriteLine("Modified: " + builder.ConnectionString); foreach (string key in builder.Keys) Console.WriteLine(key + "=" + builder[key].ToString()); Console.WriteLine("Press any key to finish."); Console.ReadLine(); } catch (System.Collections.Generic.KeyNotFoundException ex) { Console.WriteLine("KeyNotFoundException: " + ex.Message); } catch (System.FormatException ex) { Console.WriteLine("Format exception: " + ex.Message); } } }

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


SqlConnectionStringBuilder コンストラクタ

名前 | 説明 |
---|---|
SqlConnectionStringBuilder () | SqlConnectionStringBuilder クラスの新しいインスタンスを初期化します。 |
SqlConnectionStringBuilder (String) | SqlConnectionStringBuilder クラスの新しいインスタンスを初期化します。引数に渡した接続文字列によって、インスタンスの内部的な接続情報のデータが提供されます。 |

SqlConnectionStringBuilder プロパティ


SqlConnectionStringBuilder メソッド

名前 | 説明 | |
---|---|---|
![]() | Add | 指定したキーおよび値を持つエントリを DbConnectionStringBuilder に追加します。 ( DbConnectionStringBuilder から継承されます。) |
![]() | AppendKeyValuePair | オーバーロードされます。 効率的かつ安全に、既存の StringBuilder オブジェクトにキーと値を追加できます。 ( DbConnectionStringBuilder から継承されます。) |
![]() | Clear | オーバーライドされます。 SqlConnectionStringBuilder インスタンスの内容を消去します。 |
![]() | ContainsKey | オーバーライドされます。 SqlConnectionStringBuilder に特定のキーが格納されているかどうかを判断します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | EquivalentTo | この DbConnectionStringBuilder オブジェクト内の接続情報と、指定したオブジェクト内の接続情報を比較します。 ( DbConnectionStringBuilder から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Remove | オーバーライドされます。 指定したキーを持つエントリを SqlConnectionStringBuilder インスタンスから削除します。 |
![]() | ShouldSerialize | オーバーライドされます。 指定されたキーが、この SqlConnectionStringBuilder インスタンスに存在するかどうかを示します。 |
![]() | ToString | この DbConnectionStringBuilder に関連付けられている接続文字列を返します。 ( DbConnectionStringBuilder から継承されます。) |
![]() | TryGetValue | オーバーライドされます。 指定されたキーに対応する値を SqlConnectionStringBuilder から取得します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

SqlConnectionStringBuilder メンバ
SqlConnection クラスで使用する接続文字列の内容の作成と管理を簡単に実行できるようにします。
SqlConnectionStringBuilder データ型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Add | 指定したキーおよび値を持つエントリを DbConnectionStringBuilder に追加します。 (DbConnectionStringBuilder から継承されます。) |
![]() | AppendKeyValuePair | オーバーロードされます。 効率的かつ安全に、既存の StringBuilder オブジェクトにキーと値を追加できます。 (DbConnectionStringBuilder から継承されます。) |
![]() | Clear | オーバーライドされます。 SqlConnectionStringBuilder インスタンスの内容を消去します。 |
![]() | ContainsKey | オーバーライドされます。 SqlConnectionStringBuilder に特定のキーが格納されているかどうかを判断します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | EquivalentTo | この DbConnectionStringBuilder オブジェクト内の接続情報と、指定したオブジェクト内の接続情報を比較します。 (DbConnectionStringBuilder から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Remove | オーバーライドされます。 指定したキーを持つエントリを SqlConnectionStringBuilder インスタンスから削除します。 |
![]() | ShouldSerialize | オーバーライドされます。 指定されたキーが、この SqlConnectionStringBuilder インスタンスに存在するかどうかを示します。 |
![]() | ToString | この DbConnectionStringBuilder に関連付けられている接続文字列を返します。 (DbConnectionStringBuilder から継承されます。) |
![]() | TryGetValue | オーバーライドされます。 指定されたキーに対応する値を SqlConnectionStringBuilder から取得します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

Weblioに収録されているすべての辞書からSqlConnectionStringBuilderを検索する場合は、下記のリンクをクリックしてください。

- SqlConnectionStringBuilderのページへのリンク