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

keywordnull 値 (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 名前空間
その他の技術情報
接続文字列使用


このページでは「.NET Framework クラス ライブラリ リファレンス」からDbConnectionStringBuilder.TryGetValue メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDbConnectionStringBuilder.TryGetValue メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDbConnectionStringBuilder.TryGetValue メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

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

   

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



DbConnectionStringBuilder.TryGetValue メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS