ConnectionStringSettingsCollection.IndexOf(ConnectionStringSettings) Method

Definition

Returns the collection index of the passed ConnectionStringSettings object.

C#
public int IndexOf(System.Configuration.ConnectionStringSettings settings);

Parameters

settings
ConnectionStringSettings

A ConnectionStringSettings object in the collection.

Returns

The collection index of the specified ConnectionStringSettingsCollection object.

Examples

The following example shows how to get the index of the specified ConnectionStringSettings object.

C#
static void GetIndex()
{

    try
    {
        System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(
         ConfigurationUserLevel.None);

        // Clear the connection strings collection.
        ConnectionStringsSection csSection =
            config.ConnectionStrings;
        ConnectionStringSettingsCollection csCollection =
         csSection.ConnectionStrings;

        // Get the connection string setting element
        // with the specified name.
        ConnectionStringSettings cs =
            csCollection["ConnStr0"];

        // Get its index;
        int index = csCollection.IndexOf(cs);

        Console.WriteLine(
             "Connection string settings index: {0}",
             index.ToString());
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }
}

Applies to

Produkt Versioner
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also