DbConnectionStringBuilder.ConnectionString プロパティ
アセンブリ: System.Data (system.data.dll 内)

Dim instance As DbConnectionStringBuilder Dim value As String value = instance.ConnectionString instance.ConnectionString = value
/** @property */ public String get_ConnectionString () /** @property */ public void set_ConnectionString (String value)
public function get ConnectionString () : String public function set ConnectionString (value : String)
DbConnectionStringBuilder 内に格納されているキー/値ペアから作成された、現在の接続文字列。既定値は空の文字列です。


このプロパティは、DbConnectionStringBuilder により維持されるコレクションに格納されているキー/値ペアの、セミコロン区切りの一覧を返します。各ペアのキーと値は等号で区切られています。一般的な接続文字列の例を次に示します。
"Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=AdventureWorks;Data Source=(local)"
データ プロバイダでは、それぞれの接続文字列プロパティについて特定のキーと値が必要になる場合があります。そのような値は個別に記述されます。DbConnectionStringBuilder クラスでは、接続文字列に関連付けられているキー/値ペアは検証されませんが、このクラスから継承するクラスでは検証できます。
一般的に DbConnectionStringBuilder クラスの ConnectionString プロパティは、等号で区切られたキー/値ペアのセミコロン区切りの一覧の作成と解析を行う機構として使用されます。検証など、接続文字列に関するその他のサポートはありません。DbConnectionStringBuilder コレクションにアイテムを追加すると、その変更が ConnectionString プロパティに反映されます。ConnectionString プロパティに値を割り当てると、DbConnectionStringBuilder ではセミコロンと等号の区切り文字を使用して値の解析を試行します。

ConnectionString プロパティの動作を示す例を次に示します。この例では、次の操作を実行します。
-
一度に 1 つのキー/値ペアを空の DbConnectionStringBuilder に追加して、接続文字列を作成します。
-
完全な接続文字列を DbConnectionStringBuilder インスタンスの ConnectionString プロパティに割り当て、文字列内の 1 つのキー/値ペアを変更します。
-
キー/値ペアの任意のセットを ConnectionString プロパティ (つまり接続文字列とはかけ離れた文字列) に割り当て、値の 1 つを変更します。
![]() |
---|
この例には、DbConnectionStringBuilder と接続文字列がどのように連携するかを示すパスワードが含まれています。アプリケーションでは、Windows 認証の使用をお勧めします。パスワードを使用する必要がある場合は、ハードコーディングされたパスワードをアプリケーションに組み込まないでください。 |
Sub Main() ' Create a new DbConnctionStringBuilder, and add items ' to the internal collection of key/value pairs. Dim builder As New DbConnectionStringBuilder() builder.Add("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) ' Display the contents of the connection string, which ' will now contain all the key/value pairs delimited with ' semicolons. Console.WriteLine(builder.ConnectionString) Console.WriteLine() ' Clear the DbConnectionStringBuilder, and assign a complete ' connection string to it, to demonstrate how ' the class parses connection strings. builder.Clear() builder.ConnectionString = _ "Data Source=(local);Initial Catalog=AdventureWorks;" & _ "Integrated Security=SSPI" ' The DbConnectionStringBuilder class has parsed the contents, ' so you can work with any individual key/value pair. builder("Data Source") = "." Console.WriteLine(builder.ConnectionString) Console.WriteLine() ' Because the DbConnectionStringBuilder class doesn't ' validate its key/value pairs, you can use this class ' to store any semicolon-delimited list. The following ' snippet places an arbitrary string into the ConnectionString ' property, changes one of the values, and then displays the ' resulting string. builder.Clear() builder.ConnectionString = _ "Value1=10;Value2=20;Value3=30;Value4=40" builder("Value2") = 25 Console.WriteLine(builder.ConnectionString) Console.WriteLine() builder.Clear() Try ' Assigning an invalid connection string ' throws an ArgumentException. builder.ConnectionString = "xxx" Catch ex As ArgumentException Console.WriteLine("Invalid connection string.") End Try Console.WriteLine() Console.WriteLine("Press Enter to finish.") Console.ReadLine() End Sub
static void Main() { // Create a new DbConnctionStringBuilder, and add items // to the internal collection of key/value pairs. DbConnectionStringBuilder builder = new DbConnectionStringBuilder(); builder.Add("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); // Display the contents of the connection string, which // will now contain all the key/value pairs delimited with // semicolons. Console.WriteLine(builder.ConnectionString); Console.WriteLine(); // Clear the DbConnectionStringBuilder, and assign a complete // connection string to it, to demonstrate how // the class parses connection strings. builder.Clear(); builder.ConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;" + "Integrated Security=SSPI"; // The DbConnectionStringBuilder class has parsed the contents, // so you can work with any individual key/value pair. builder["Data Source"] = "."; Console.WriteLine(builder.ConnectionString); Console.WriteLine(); // Because the DbConnectionStringBuilder class doesn't // validate its key/value pairs, you can use this class // to store any semicolon-delimited list. The following // snippet places an arbitrary string into the ConnectionString // property, changes one of the values, and then displays the // resulting string. builder.Clear(); builder.ConnectionString = "Value1=10;Value2=20;Value3=30;Value4=40"; builder["Value2"] = 25; Console.WriteLine(builder.ConnectionString); Console.WriteLine(); builder.Clear(); try { // Assigning an invalid connection string // throws an ArgumentException. builder.ConnectionString = "xxx"; } catch (ArgumentException) { Console.WriteLine("Invalid connection string."); } Console.WriteLine(); Console.WriteLine("Press Enter to finish."); Console.ReadLine(); }

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.ConnectionString プロパティのページへのリンク