共用方式為


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> 表示系統會忽略父組態檔中定義的所有連接字串。

適用於

另請參閱