ConnectionStringSettingsCollection.Clear 方法

定义

从集合中移除所有 ConnectionStringSettings 对象。

public:
 void Clear();
public void Clear ();
member this.Clear : unit -> unit
Public Sub Clear ()

示例

以下示例演示如何从ConnectionStringSettingsCollection集合中删除所有 ConnectionStringSettings 对象。

// 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.");
}
' Clear connection strings collection.
Shared Sub ClearConnectionStrings() 
    
    ' Get the application configuration file.
    Dim config _
    As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration( _
    ConfigurationUserLevel.None)

    ' Clear the connection strings collection.
    Dim csSection _
    As ConnectionStringsSection = _
    config.ConnectionStrings
    csSection.ConnectionStrings.Clear()
    
    ' Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified)
    
    Console.WriteLine("Connection strings cleared.")

End Sub

注解

方法 Clear 从当前配置层次结构级别的配置文件中删除所有连接字符串,并在文件中写入标记 <clear> 。 标记 <clear> 表示系统忽略父配置文件中定义的所有连接字符串。

适用于

另请参阅