OleDbConnectionStringBuilder.TryGetValue Method (String, Object)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Retrieves a value corresponding to the supplied key from the OleDbConnectionStringBuilder instance.

Namespace:   System.Data.OleDb
Assembly:  System.Data (in System.Data.dll)

public override bool TryGetValue(
	string keyword,
	out object value


Type: System.String

The key of the item to retrieve.

Type: System.Object

The value corresponding to keyword.

Return Value

Type: System.Boolean

true if keyword was found within the connection string; otherwise false.

Exception Condition

keyword contains a null value (Nothing in Visual Basic).

The TryGetValue method lets developers safely retrieve a value from a OleDbConnectionStringBuilder without needing to verify that the supplied key name is a valid key name. Because TryGetValue does not raise an exception when you call it, passing in a nonexistent key, you do not have to look for a key before retrieving its value. Calling TryGetValue with a nonexistent key will place the value null (Nothing in Visual Basic) in the value parameter.

The following example demonstrates the behavior of the TryGetValue method.

using System.Data.OleDb;

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

        // Call TryGetValue method for multiple
        // key names. 
        DisplayValue(builder, "Data Source");
        DisplayValue(builder, "Extended Properties");
        // How about implicitly added key/value pairs?
        DisplayValue(builder, "Jet OLEDB:System database");
        // Invalid keys?
        DisplayValue(builder, "Invalid Key");
        // Null values?
        DisplayValue(builder, null);

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

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

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

    static private string GetConnectionString()
        // To avoid storing the connection string in your code,
        // you can retrieve it from a configuration file using the 
        // System.Configuration.ConfigurationSettings.AppSettings property. 
        return "Provider=Microsoft.Jet.OLEDB.4.0;" +
            "Data Source=C:\\ExcelDemo.xls;" +
            "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";

.NET Framework
Available since 2.0
Return to top