Επεξεργασία

Κοινή χρήση μέσω


OdbcConnectionStringBuilder.Remove(String) Method

Definition

Removes the entry with the specified key from the OdbcConnectionStringBuilder instance.

public:
 override bool Remove(System::String ^ keyword);
public override bool Remove (string keyword);
override this.Remove : string -> bool
Public Overrides Function Remove (keyword As String) As Boolean

Parameters

keyword
String

The key of the key/value pair to be removed from the connection string in this OdbcConnectionStringBuilder.

Returns

true if the key existed within the connection string and was removed; false if the key did not exist.

Exceptions

keyword is null (Nothing in Visual Basic).

Examples

The following example creates an OdbcConnectionStringBuilder instance and demonstrates the behavior of the Remove method.

using System.Data.Odbc;

class Program
{
    static void Main()
    {
        OdbcConnectionStringBuilder builder =
            new OdbcConnectionStringBuilder();
        builder.ConnectionString =
            "Driver={SQL Server};Server=(local);" +
            "Database=AdventureWorks;Trusted_Connection=yes;";

        Console.WriteLine(builder.ConnectionString);

        // Try to remove an existing item.
        TryRemove(builder, "Server");

        // Try to remove a nonexistent item.
        TryRemove(builder, "User ID");

        // Try to remove an existing item,
        // demonstrating that the search is not
        // case sensitive.
        TryRemove(builder, "DATABASE");

        Console.WriteLine("Press Enter to continue.");
        Console.ReadLine();
    }

    static void TryRemove(OdbcConnectionStringBuilder builder,
        string itemToRemove)
    {

        if (builder.Remove(itemToRemove))
        {
            Console.WriteLine("Removed '{0}'", itemToRemove);
        }
        else
        {
            Console.WriteLine("Unable to remove '{0}'", itemToRemove);
        }
        Console.WriteLine(builder.ConnectionString);
    }
}
Imports System.Data.Odbc  
  
Module Module1
  Sub Main()
    Dim builder As New OdbcConnectionStringBuilder
    builder.ConnectionString = _
     "Driver={SQL Server};Server=(local);" & _
     "Database=AdventureWorks;Trusted_Connection=yes;"

    Console.WriteLine(builder.ConnectionString)

    ' Try to remove an existing item.
    TryRemove(builder, "Server")

    ' Try to remove a nonexistent item.
    TryRemove(builder, "User ID")

    ' Try to remove an existing item, 
    ' demonstrating that the search is not 
    ' case sensitive.
    TryRemove(builder, "DATABASE")

    Console.WriteLine("Press Enter to continue.")
    Console.ReadLine()
  End Sub

  Sub TryRemove(ByVal builder As OdbcConnectionStringBuilder, _
      ByVal itemToRemove As String)

    If builder.Remove(itemToRemove) Then
      Console.WriteLine("Removed '{0}'", itemToRemove)
    Else
      Console.WriteLine("Unable to remove '{0}'", itemToRemove)
    End If
    Console.WriteLine(builder.ConnectionString)
  End Sub
End Module

Remarks

Because the Remove method returns a value that indicates its success, it is not required to look for the existence of a key before trying to remove the key/value pair from the OdbcConnectionStringBuilder instance.

Applies to

See also