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

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

SqlConnectionStringBuilder.TryGetValue メソッド

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

指定されキー対応する値を SqlConnectionStringBuilder から取得します

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

Public Overrides Function
 TryGetValue ( _
    keyword As String, _
    <OutAttribute> ByRef value As Object
 _
) As Boolean
Dim instance As SqlConnectionStringBuilder
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
) override
public boolean TryGetValue (
    String keyword, 
    /** @attribute OutAttribute() */ /** @ref */ Object value
)
JScript では、値型引数参照渡しされません。

パラメータ

keyword

取得する項目のキー

value

keyword.対応する値。

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

例外例外
例外種類条件

ArgumentNullException

keywordnull 値 (Visual Basic の場合Nothing) が含まれています。

解説解説

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

使用例使用例

TryGetValue メソッド動作を示す例を次に示します

Imports System.Data.SqlClient
    
Module Module1
    Sub Main()
        Dim builder As New
 SqlConnectionStringBuilder
        builder.ConnectionString = GetConnectionString()

        ' Call TryGetValue method for multiple
        ' key names. Note that these keys are converted
        ' to well-known synonynms for data retrieval.
        DisplayValue(builder, "Data Source")
        DisplayValue(builder, "Trusted_Connection")
        DisplayValue(builder, "InvalidKey")
        DisplayValue(builder, Nothing)

        Console.WriteLine("Press any key to continue.")
        Console.ReadLine()
    End Sub

    Private Sub DisplayValue( _
     ByVal builder As SqlConnectionStringBuilder,
 ByVal key As String)
        Dim value As Object
 = Nothing

        ' 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
        ' passes 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

    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.SqlClient;

using System;
using System.Data.SqlClient;


class Program
{
    static void Main()
    {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.ConnectionString = GetConnectionString();

        // Call TryGetValue method for multiple
        // key names. Note that these keys are converted
        // to well-known synonynms for data retrieval.
        DisplayValue(builder, "Data Source");
        DisplayValue(builder, "Trusted_Connection");
        DisplayValue(builder, "InvalidKey");
        DisplayValue(builder, null);

        Console.WriteLine("Press any key to continue.");
        Console.ReadLine();
    }

    private static void
 DisplayValue(
        SqlConnectionStringBuilder 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
        // passes 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.");
        }
    }

    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";
    }
}

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

Data Source=(local)
Trusted_Connection=True
Unable to retrieve value for 'InvalidKey'
Unable to retrieve value for null key.
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SqlConnectionStringBuilder クラス
SqlConnectionStringBuilder メンバ
System.Data.SqlClient 名前空間
その他の技術情報
接続文字列使用



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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2024 GRAS Group, Inc.RSS