SqlConnectionStringBuilder.Remove(String) Method

Definition

SqlConnectionStringBuilder 实例中移除具有指定的键的项。Removes the entry with the specified key from the SqlConnectionStringBuilder 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

要从此 SqlConnectionStringBuilder 中的连接字符串移除的键/值对中的键。The key of the key/value pair to be removed from the connection string in this SqlConnectionStringBuilder.

Returns

Boolean

如果该键存在于连接字符串中并已被移除,则为 true;如果该键不存在,则为 falsetrue if the key existed within the connection string and was removed; false if the key did not exist.

Exceptions

keyword 为 Null(在 Visual Basic 中为 Nothingkeyword is null (Nothing in Visual Basic)

Examples

以下示例将现有连接字符串从使用 Windows 身份验证转换为使用集成安全性。The following example converts an existing connection string from using Windows Authentication to using integrated security. 该示例通过以下方式来完成此工作:从连接字符串中移除用户名和密码,然后设置 IntegratedSecurity 对象的 SqlConnectionStringBuilder 属性。The example works by removing the user name and password from the connection string, and then setting the IntegratedSecurity property of the SqlConnectionStringBuilder object.

Note

该示例包括一个密码以演示 SqlConnectionStringBuilder 如何使用连接字符串。This example includes a password to demonstrate how SqlConnectionStringBuilder works with connection strings. 在您的应用程序中,建议使用 Windows 身份验证。In your applications, we recommend that you use Windows Authentication. 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。If you must use a password, do not include a hard-coded password in your application.

using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        try
        {
            string connectString =
                "Data Source=(local);User ID=ab;Password= a1Pass@@11;" +
                "Initial Catalog=AdventureWorks";

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
            Console.WriteLine("Original: " + builder.ConnectionString);

            // Use the Remove method
            // in order to reset the user ID and password back to their
            // default (empty string) values.
            builder.Remove("User ID");
            builder.Remove("Password");

            // Turn on integrated security:
            builder.IntegratedSecurity = true;

            Console.WriteLine("Modified: " + builder.ConnectionString);

            using (SqlConnection
                       connection = new SqlConnection(builder.ConnectionString))
            {
                connection.Open();
                // Now use the open connection.
                Console.WriteLine("Database = " + connection.Database);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.WriteLine("Press any key to finish.");
        Console.ReadLine();
    }
}
Imports System.Data.SqlClient
    
Module Module1
  Sub Main()
    Try
      Dim connectString As String = _
       "Data Source=(local);User ID=ab;Password=a1Pass@@11;" & _
       "Initial Catalog=AdventureWorks"

      Dim builder As New SqlConnectionStringBuilder(connectString)
      Console.WriteLine("Original: " & builder.ConnectionString)

      ' Use the Remove method
      ' in order to reset the user ID and password back to their
      ' default (empty string) values.
      builder.Remove("User ID")
      builder.Remove("Password")

      ' Turn on integrated security:
      builder.IntegratedSecurity = True

      Console.WriteLine("Modified: " & builder.ConnectionString)

      Using connection As New SqlConnection(builder.ConnectionString)
        connection.Open()
        ' Now use the open connection.
        Console.WriteLine("Database = " & connection.Database)
      End Using

    Catch ex As Exception
      Console.WriteLine(ex.Message)
    End Try

    Console.WriteLine("Press any key to finish.")
    Console.ReadLine()
  End Sub
End Module

该示例在控制台窗口中显示以下文本:The example displays the following text in the console window:

Original: Data Source=(local);Initial Catalog=AdventureWorks;User ID=ab;Password= a1Pass@@11
Modified: Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True
Database = AdventureWorks

Remarks

由于Remove方法会返回一个指示其成功的值,因此在尝试从 SqlConnectionStringBuilder 实例中删除键/值对之前,不需要查找键。Because the Remove method returns a value that indicates its success, it is not required to look for a key before trying to remove the key/value pair from the SqlConnectionStringBuilder instance. 由于 SqlConnectionStringBuilder 保持固定大小的键/值对集合,因此调用 Remove 方法只会将键/值对的值重置回其默认值。Because the SqlConnectionStringBuilder maintains a fixed-size collection of key/value pairs, calling the Remove method simply resets the value of the key/value pair back to its default value.

由于 SqlConnectionStringBuilder 所支持的键集合是固定的,因此该集合中的每一项都具有已知的默认值。Because the collection of keys supported by the SqlConnectionStringBuilder is fixed, every item within the collection has a known default value. 下表列出了这些键,以及首次初始化 SqlConnectionStringBuilder 时或调用 Remove 方法后每个键的值。The following table lists the keys, and the value for each when the SqlConnectionStringBuilder is first initialized, or after the Remove method has been called.

密钥Key 默认值Default value
应用程序名称Application Name ".Net SqlClient Data Provider"".Net SqlClient Data Provider"
Asynchronous ProcessingAsynchronous Processing FalseFalse
AttachDBFilenameAttachDBFilename 空字符串Empty string
连接超时值Connection Timeout 1515
上下文连接Context Connection FalseFalse
当前语言Current Language 空字符串Empty string
数据源Data Source 空字符串Empty string
加密Encrypt FalseFalse
登记Enlist TrueTrue
Failover PartnerFailover Partner 空字符串Empty string
初始目录Initial Catalog 空字符串Empty string
集成安全性Integrated Security FalseFalse
Load Balance TimeoutLoad Balance Timeout 00
最大池大小Max Pool Size 100100
Min Pool SizeMin Pool Size 00
MultipleActiveResultSetsMultipleActiveResultSets FalseFalse
Network LibraryNetwork Library 空字符串Empty string
Packet SizePacket Size 80008000
密码Password 空字符串Empty string
持久性安全信息Persist Security Info FalseFalse
PoolingPooling TrueTrue
复制Replication FalseFalse
事务绑定Transaction Binding Implicit UnbindImplicit Unbind
用户 IDUser ID 空字符串Empty string
User InstanceUser Instance FalseFalse
工作站 IDWorkstation ID 空字符串Empty string

Applies to

See also