SqlConnectionStringBuilder Class (System - Data)
SqlConnectionStringBuilder Class (System - Data)
SqlClient)
SqlConnectionStringBuilder Class
.NET Framework 4.5 This topic has not yet been rated
Provides a simple way to create and manage the contents of connection strings used by the SqlConnection class.
Inheritance Hierarchy
System.Object
System.Data.Common.DbConnectionStringBuilder
System.Data.SqlClient.SqlConnectionStringBuilder
Namespace: System.Data.SqlClient
Assembly: System.Data (in System.Data.dll)
Syntax
C#
Constructors
Name Description
Top
Properties
Name Description
ApplicationName Gets or sets the name of the application associated with the
connection string.
AttachDBFilename Gets or sets a string that contains the name of the primary data file.
This includes the full path name of an attachable database.
ConnectTimeout Gets or sets the length of time (in seconds) to wait for a connection
to the server before terminating the attempt and generating an error.
Count Gets the current number of keys that are contained within the
ConnectionString property. (Inherited from
DbConnectionStringBuilder.)
DataSource Gets or sets the name or network address of the instance of SQL
Server to connect to.
Encrypt Gets or sets a Boolean value that indicates whether SQL Server uses
SSL encryption for all data sent between the client and server if the
server has a certificate installed.
Enlist Gets or sets a Boolean value that indicates whether the SQL Server
connection pooler automatically enlists the connection in the
creation thread's current transaction context.
FailoverPartner Gets or sets the name or address of the partner server to connect to if
the primary server is down.
msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx 2/10
23/02/13 SqlConnectionStringBuilder Class (System.Data.SqlClient)
InitialCatalog Gets or sets the name of the database associated with the
connection.
IntegratedSecurity Gets or sets a Boolean value that indicates whether User ID and
Password are specified in the connection (when false) or whether
the current Windows account credentials are used for authentication
(when true).
Item Gets or sets the value associated with the specified key. In C#, this
property is the indexer. (Overrides DbConnectionStringBuilder.Item.)
LoadBalanceTimeout Gets or sets the minimum time, in seconds, for the connection to
live in the connection pool before being destroyed.
MultipleActiveResultSets When true, an application can maintain multiple active result sets
(MARS). When false, an application must process or cancel all result
sets from one batch before it can execute any other batch on that
connection.For more information, see Multiple Active Result Sets
(MARS).
NetworkLibrary Gets or sets a string that contains the name of the network library
used to establish a connection to the SQL Server.
PacketSize Gets or sets the size in bytes of the network packets used to
communicate with an instance of SQL Server.
Password Gets or sets the password for the SQL Server account.
Pooling Gets or sets a Boolean value that indicates whether the connection
will be pooled or explicitly opened every time that the connection is
requested.
TransactionBinding Gets or sets a string value that indicates how the connection
maintains its association with an enlisted System.Transactions
transaction.
TrustServerCertificate Gets or sets a value that indicates whether the channel will be
encrypted while bypassing walking the certificate chain to validate
trust.
TypeSystemVersion Gets or sets a string value that indicates the type system the
application expects.
UserID Gets or sets the user ID to be used when connecting to SQL Server.
UserInstance Gets or sets a value that indicates whether to redirect the connection
from the default SQL Server Express instance to a runtime-initiated
instance running under the account of the caller.
WorkstationID Gets or sets the name of the workstation connecting to SQL Server.
Top
Methods
Name Description
Add Adds an entry with the specified key and value into the
DbConnectionStringBuilder. (Inherited from
DbConnectionStringBuilder.)
Equals(Object) Determines whether the specified object is equal to the current object.
(Inherited from Object.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
Remove Removes the entry with the specified key from the
SqlConnectionStringBuilder instance. (Overrides
DbConnectionStringBuilder.Remove(String).)
Top
Name Description
msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx 5/10
23/02/13 SqlConnectionStringBuilder Class (System.Data.SqlClient)
msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx 6/10
23/02/13 SqlConnectionStringBuilder Class (System.Data.SqlClient)
Top
Remarks
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 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 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 Remove method. See the 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]
[C#]
msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx 7/10
23/02/13 SqlConnectionStringBuilder Class (System.Data.SqlClient)
System.Data.SqlClient.SqlConnectionStringBuilder builder =
new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "(local)";
builder["integrated Security"] = true;
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";
Console.WriteLine(builder.ConnectionString);
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
Examples
The following console application builds connection strings for a SQL Server database. The code uses a
SqlConnectionStringBuilder class to create the connection string, and then passes the ConnectionString property of the
SqlConnectionStringBuilder instance to the constructor of the connection class. The example also parses an existing
connection string and demonstrates various ways of manipulating the connection string's contents.
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 System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
// Create a new SqlConnectionStringBuilder and
// initialize it with a few name/value pairs.
SqlConnectionStringBuilder builder =
new SqlConnectionStringBuilder(GetConnectionString());
Version Information
.NET Framework
Supported in: 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client Profile
Supported in: 4, 3.5 SP1
Platforms
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not
supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET
Framework System Requirements.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not
guaranteed to be thread safe.
msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx 9/10
23/02/13 SqlConnectionStringBuilder Class (System.Data.SqlClient)
guaranteed to be thread safe.
See Also
Reference
System.Data.SqlClient Namespace
Other Resources
Connection Strings (ADO.NET)
ADO.NET Managed Providers and DataSet Developer Center
msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx 10/10
Using SqlConnectionStringBuilder class in C#
Posted by Waqas Anwar on 16 May 2008 in ADO.NET
inShare
Microsoft introduced a new class in ADO.NET 2.0 version which can be used to build database connection strings
specific to the provider you are using in ADO.NET. Different database providers expose different properties for the
connection strings. For example, SQL Server connection string properties are different than MySql or OleDb
connection strings. Although, it is possible to concatenate strings to build a complete connection string but by using
.NET connection string builder class you don't need to memorize the properties appropriate to specific provider.
In the following tutorial, I will show you how you can use SqlConnectionStringBuilder class available
inSystem.Data.SqlClient namespace to build a simple connection string.
builder.DataSource = TextBox1.Text;
builder.InitialCatalog = TextBox2.Text;
builder.UserID = TextBox3.Text;
builder.Password = TextBox4.Text;
TextBox5.Text = builder.ConnectionString;
Other than above mentioned four basic properties SqlConnectionStringBuilder also provides following common
properties which can be used to build connection string with more options.
AsynchronousProcessing
AttachDBFilename
IntegratedSecurity
MaxPoolSize
MinPoolSize
MultipleActiveResultSets
PacketSize
Pooling
UserInstance
WorkstationID