System.Data.SqlClient.SqlConnectionStringBuilder Class

Provides a simple way to create and manage the contents of connection strings used by the System.Data.SqlClient.SqlConnection class.

See Also: SqlConnectionStringBuilder Members


[System.ComponentModel.TypeConverter("System.Data.SqlClient.SqlConnectionStringBuilder+SqlConnectionStringBuilderConverter, System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class SqlConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder


The connection string builder lets developers programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings, using properties and methods of the class. The connection string builder provides strongly typed properties corresponding to the known key/value pairs allowed by SQL Server. Developers needing to create connection strings as part of applications can use the System.Data.SqlClient.SqlConnectionStringBuilder class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file.

The System.Data.SqlClient.SqlConnectionStringBuilder performs checks for valid key/value pairs. Therefore, you cannot use this class to create invalid connection strings; trying to add invalid pairs will throw an exception. The class maintains a fixed collection of synonyms and can translate from a synonym to the corresponding well-known key name.

For example, when you use the Item property to retrieve a value, you can specify a string that contains any synonym for the key you need. For example, you can specify "Network Address", "addr", or any other acceptable synonym for this key within a connection string when you use any member that requires a string that contains the key name, such as the Item property or the SqlConnectionStringBuilder.Remove(string) method. See the SqlConnection.ConnectionString property for a full list of acceptable synonyms.

The Item property handles tries to insert malicious entries. For example, the following code, using the default Item property (the indexer, in C#) correctly escapes the nested key/value pair:

[Visual Basic]


Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
builder("Data Source") = "(local)"
builder("Integrated Security") = True
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"



System.Data.SqlClient.SqlConnectionStringBuilder builder =
  new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";

The result is the following connection string that handles the invalid value in a safe manner:


Source=(local);Initial Catalog="AdventureWorks;NewValue=Bad";
Integrated Security=True


Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)
Assembly Versions:
Since: .NET 2.0