ConnectionStringSettingsCollection.Clear Method ()

 
System_CAPS_noteNote

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

Removes all the ConnectionStringSettings objects from the collection.

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)

public void Clear()

The Clear method removes all the connection strings from the configuration file at the current configuration hierarchy level and writes a <clear> tag in the file. The <clear> tag signifies that all the connection strings defined in the parent configuration files are disregarded by the system.

The following example shows how to remove all the ConnectionStringSettings objects from the ConnectionStringSettingsCollection collection.

// Clear connection strings collection.
static void ClearConnectionStrings()
{

    // Get the configuration file.
    System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.None);

    // Clear the connection strings collection.
    ConnectionStringsSection csSection =
        config.ConnectionStrings;
    csSection.ConnectionStrings.Clear();

    // Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified);

    Console.WriteLine("Connection strings cleared.");


}

.NET Framework
Available since 2.0
Return to top
Show: