SqlConnectionStringBuilder Constructors

Definition

初始化 SqlConnectionStringBuilder 类的新实例。Initializes a new instance of the SqlConnectionStringBuilder class.

Overloads

SqlConnectionStringBuilder()

初始化 SqlConnectionStringBuilder 类的新实例。Initializes a new instance of the SqlConnectionStringBuilder class.

SqlConnectionStringBuilder(String)

初始化 SqlConnectionStringBuilder 类的新实例。Initializes a new instance of the SqlConnectionStringBuilder class. 所提供的连接字符串为实例的内部连接信息提供数据。The provided connection string provides the data for the instance's internal connection information.

SqlConnectionStringBuilder()

初始化 SqlConnectionStringBuilder 类的新实例。Initializes a new instance of the SqlConnectionStringBuilder class.

public:
 SqlConnectionStringBuilder();
public SqlConnectionStringBuilder ();
Public Sub New ()

See also

SqlConnectionStringBuilder(String)

初始化 SqlConnectionStringBuilder 类的新实例。Initializes a new instance of the SqlConnectionStringBuilder class. 所提供的连接字符串为实例的内部连接信息提供数据。The provided connection string provides the data for the instance's internal connection information.

public:
 SqlConnectionStringBuilder(System::String ^ connectionString);
public SqlConnectionStringBuilder (string connectionString);
new System.Data.SqlClient.SqlConnectionStringBuilder : string -> System.Data.SqlClient.SqlConnectionStringBuilder
Public Sub New (connectionString As String)

Parameters

connectionString
String

对象内部连接信息的基础。The basis for the object's internal connection information. 分析为名称/值对。Parsed into name/value pairs. 无效的键名会引发 KeyNotFoundExceptionInvalid key names raise KeyNotFoundException.

Exceptions

连接字符串中包含无效的键名。Invalid key name within the connection string.

连接字符串中包含无效的值(具体而言,需要使用布尔值或数值,但字符串中并未提供)。Invalid value within the connection string (specifically, when a Boolean or numeric value was expected but not supplied).

所提供的 connectionString 无效。The supplied connectionString is not valid.

Examples

以下示例在 SqlConnectionStringBuilder 对象的构造函数中提供一个简单的 SQL Server 连接字符串,然后循环访问该对象中的所有键/值对。The following example supplies a simple SQL Server connection string in the SqlConnectionStringBuilder object's constructor, and then iterates through all the key/value pairs within the object. 请注意,该集合提供每一项的默认值。Note that the collection provides default values for each item. 另请注意,SqlConnectionStringBuilder 类会转换已知键的同义词,使其与已知名称一致。Also note that the SqlConnectionStringBuilder class converts synonyms for the well-known keys so that they are consistent with the well-known names.

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 =
                "Server=(local);Database=AdventureWorks;UID=ab;Pwd= a!Pass@@";
            Console.WriteLine("Original: " + connectString);
            SqlConnectionStringBuilder builder =
                new SqlConnectionStringBuilder(connectString);
            Console.WriteLine("Modified: " + builder.ConnectionString);
            foreach (string key in builder.Keys)
                Console.WriteLine(key + "=" + builder[key].ToString());
            Console.WriteLine("Press any key to finish.");
            Console.ReadLine();
        }
        catch (System.Collections.Generic.KeyNotFoundException ex)
        {
            Console.WriteLine("KeyNotFoundException: " + ex.Message);
        }
        catch (System.FormatException ex)
        {
            Console.WriteLine("Format exception: " + ex.Message);
        }
    }
}
Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Try
            Dim connectString As String = _
                "Server=(local);Database=AdventureWorks;UID=ab;Pwd=a!Pass@@"
            Console.WriteLine("Original: " & connectString)
            Dim builder As New SqlConnectionStringBuilder(connectString)
            Console.WriteLine("Modified: " & builder.ConnectionString)
            For Each key As String In builder.Keys
                Console.WriteLine(key & "=" & builder.Item(key).ToString)
            Next
            Console.WriteLine("Press any key to finish.")
            Console.ReadLine()

        Catch ex As System.Collections.Generic.KeyNotFoundException
            Console.WriteLine("KeyNotFoundException: " & ex.Message)
        Catch ex As System.FormatException
            Console.WriteLine("Format exception: " & ex.Message)
        End Try
    End Sub
End Module 

Remarks

SqlConnectionStringBuilder 类提供键/值对的固定内部集合。The SqlConnectionStringBuilder class provides a fixed internal collection of key/value pairs. 即使您只提供构造函数中可能的连接字符串值的一个小子集,该对象也会始终提供每个键/值对的默认值。Even if you supply only a small subset of the possible connection string values in the constructor, the object always provides default values for each key/value pair. 检索对象的 ConnectionString 属性时,字符串仅包含值不是项的默认值的键/值对。When the ConnectionString property of the object is retrieved, the string contains only key/value pairs in which the value is not the default value for the item.

See also

Applies to