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 クラスのページへのリンク