SqlConnectionStringBuilder Constructors

Definition

Overloads

SqlConnectionStringBuilder()

Initializes a new instance of the SqlConnectionStringBuilder class.

SqlConnectionStringBuilder(String)

Initializes a new instance of the SqlConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.

SqlConnectionStringBuilder()

Initializes a new instance of the SqlConnectionStringBuilder class.

C#
public SqlConnectionStringBuilder ();

Applies to

SqlClient .NET Core 5.2 and other versions
Product Versions
SqlClient .NET Core 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2
SqlClient .NET Framework 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2
SqlClient .NET Standard 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2

SqlConnectionStringBuilder(String)

Initializes a new instance of the SqlConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.

C#
public SqlConnectionStringBuilder (string connectionString);

Parameters

connectionString
String

The basis for the object's internal connection information. Parsed into name/value pairs. Invalid 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).

The supplied connectionString is not valid.

Examples

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. Also note that the SqlConnectionStringBuilder class converts synonyms for the well-known keys so that they are consistent with the well-known names.

Note

This example includes a password to demonstrate how SqlConnectionStringBuilder works with connection strings. 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.

C#
using Microsoft.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);
        }
    }
}

Remarks

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. 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.

Applies to

SqlClient .NET Core 5.2 and other versions
Product Versions
SqlClient .NET Core 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2
SqlClient .NET Framework 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2
SqlClient .NET Standard 1.0, 1.1, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0, 5.1, 5.2