DbConnectionStringBuilder.TryGetValue メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DbConnectionStringBuilder.TryGetValue メソッドの意味・解説 

DbConnectionStringBuilder.TryGetValue メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定されたキーに対応する値を DbConnectionStringBuilder から取得します。

名前空間: System.Data.Common
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Overridable Function
 TryGetValue ( _
    keyword As String, _
    <OutAttribute> ByRef value As Object
 _
) As Boolean
Dim instance As DbConnectionStringBuilder
Dim keyword As String
Dim value As Object
Dim returnValue As Boolean

returnValue = instance.TryGetValue(keyword, value)
public:
virtual bool TryGetValue (
    String^ keyword, 
    [OutAttribute] Object^% value
)
public boolean TryGetValue (
    String keyword, 
    /** @attribute OutAttribute() */ /** @ref */ Object value
)
JScript では、値型の引数は参照渡しされません。

パラメータ

keyword

取得する項目のキー。

value

key に対応する値。

戻り値
keyword が接続文字列に存在する場合は true。それ以外の場合は false。

例外例外
例外の種類条件

ArgumentNullException

keyword に null 値 (Visual Basic では Nothing) が含まれています。

解説解説

TryGetValue メソッドを使用すると、DbConnectionStringBuilder から値を安全に取得でき、最初に ContainsKey メソッドを呼び出す必要がなくなります。TryGetValue を呼び出して存在しないキーを渡しても、例外が発生しないため、対応する値を取得する前にキーを検索する必要はありません。存在しないキーを使用して TryGetValue を呼び出すと、value パラメータには null 値 (Visual Basic では Nothing) が設定されます。

使用例使用例

次に示す例では TryGetValue メソッドを呼び出し、その結果をいくつか示します。

Sub Main()
    Dim builder As New DbConnectionStringBuilder
    builder.ConnectionString = _
        "Provider=sqloledb;Data Source=192.168.168.1,1433;"
 & _
        "Network Library=DBMSSOCN;Initial Catalog=pubs;"
 & _
        "Integrated Security=SSPI;"

    ' Call TryGetValue method for multiple
    ' key names.
    DisplayValue(builder, "Provider")
    DisplayValue(builder, "DATA SOURCE")
    DisplayValue(builder, "InvalidKey")
    DisplayValue(builder, Nothing)

    Console.ReadLine()
End Sub

Private Sub DisplayValue( _
 ByVal builder As DbConnectionStringBuilder,
 ByVal key As String)
    Dim value As Object

    ' Although TryGetValue handles missing keys,
    ' it doesn't handle passing in a null (Nothing in Visual Basic)
    ' key. This example traps for that particular error, but
    ' bubbles any other unknown exceptions back out to the
    ' caller. 
    Try
        If builder.TryGetValue(key, value) Then
            Console.WriteLine("{0}={1}", key, value)
        Else
            Console.WriteLine("Unable to retrieve value for
 '{0}'", key)
        End If
    Catch ex As ArgumentNullException
        Console.WriteLine("Unable to retrieve value for null key.")
    End Try
End Sub
static void Main()
{
    DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
    builder.ConnectionString = 
        "Provider=sqloledb;Data Source=192.168.168.1,1433;" +
        "Network Library=DBMSSOCN;Initial Catalog=pubs;" +
        "Integrated Security=SSPI;";

    // Call TryGetValue method for multiple
    // key names.
    DisplayValue(builder, "Provider");
    DisplayValue(builder, "DATA SOURCE");
    DisplayValue(builder, "InvalidKey");
    DisplayValue(builder, null);

    Console.ReadLine();
}

private static void DisplayValue(
    DbConnectionStringBuilder builder, string key)
{
    object value = null;

    // Although TryGetValue handles missing keys,
    // it doesn't handle passing in a null 
    // key. This example traps for that particular error, but
    // bubbles any other unknown exceptions back out to the
    // caller. 
    try
    {
        if (builder.TryGetValue(key, out value))
        {
            Console.WriteLine("{0}={1}", key, value);
        }
        else
        {
            Console.WriteLine(@"Unable to retrieve value for
 '{0}'", key);
        }
    }
    catch (ArgumentNullException)
    {
        Console.WriteLine("Unable to retrieve value for null
 key.");
    }
}

このサンプルでは、次の結果が表示されます。

Provider=sqloledb
DATA SOURCE=192.168.168.1,1433
Unable to retrieve value for //InvalidKey//
Unable to retrieve value for null key.
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DbConnectionStringBuilder クラス
DbConnectionStringBuilder メンバ
System.Data.Common 名前空間
その他の技術情報
接続文字列の使用



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

DbConnectionStringBuilder.TryGetValue メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DbConnectionStringBuilder.TryGetValue メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS